建立預先簽署的 URL

IBM Cloud® Object Storage 中的預先簽署 URL 會建立暫時鏈結,可用來共用物件,在存取時不需要其他使用者認證。

當然,您也可以 提供暫時目標以傳送 PUT 要求,而不需要提供任何其他資訊來進行鑑別。 建立預先簽署 URL 最簡單的方法是使用 AWS CLI。 但首先,您可能需要執行 aws configure,才能從您自己的 啟用 HMAC 的服務認證 設定「存取金鑰 ID」及「秘密存取金鑰」。 當您完成配置 CLI 時,請使用下列範例作為範本,並將儲存區的端點及名稱取代為適當的資訊:

$ aws --endpoint-url=https://{endpoint} s3 presign s3://{bucket-name}/{new-file-key}

如果刪除用來產生 HMAC 認證 (用作上述「存取金鑰 ID」及「秘密存取金鑰」配置) 的服務認證,則預先簽署 URL 的存取將會失敗。

您也可以設定 URL 的有效期限(以秒為單位,預設值為 3600):

$ aws --endpoint-url=https://{endpoint} s3 presign s3://bucket-1/new-file --expires-in 600

您也可以以程式設計方式來建構它們。 以下是以 Python 撰寫的基本 GET 作業範例。 如需端點的相關資訊,請參閱端點及儲存空間位置

與 AWS S3不同,IBM Cloud Object Storage 不會強制有效期限上限為 7 天 (604800 秒)。 雖然可以建立具有長有效期限值的預先簽署 URL,但在儲存區上 實作公用存取原則 將更適合需要延伸公用存取權的大部分使用案例。

建立預先簽署的 URL 以下載物件

Python 範例

import ibm_boto3
import os

bucket_name = '<bucekt name>'
key_name = '<object key name>'
http_method = 'get_object'
expiration = 600  # time in seconds, default:600

access_key = os.environ.get('COS_HMAC_ACCESS_KEY_ID')
secret_key = os.environ.get('COS_HMAC_SECRET_ACCESS_KEY')
# Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints
cos_service_endpoint = 'https://s3.<region>.cloud-object-storage.appdomain.cloud'

cos = ibm_boto3.client("s3",
                       aws_access_key_id=access_key,
                       aws_secret_access_key=secret_key,
                       endpoint_url=cos_service_endpoint
                       )

signedUrl = cos.generate_presigned_url(http_method, Params={
                                       'Bucket': bucket_name, 'Key': key_name}, ExpiresIn=expiration)
print("presigned download URL =>" + signedUrl)

Java 範例

import java.util.Date;
import com.ibm.cloud.objectstorage.auth.AWSCredentials;
import com.ibm.cloud.objectstorage.auth.AWSStaticCredentialsProvider;
import com.ibm.cloud.objectstorage.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.ibm.cloud.objectstorage.HttpMethod;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3ClientBuilder;
import com.ibm.cloud.objectstorage.services.s3.model.GeneratePresignedUrlRequest;


String bucketName = "<bucket name>";
String keyName = "<object key name>";
HttpMethod httpMethod = HttpMethod.GET;
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60;
expiration.setTime(expTimeMillis);

String accessKey = "<COS_HMAC_ACCESS_KEY_ID>";
String secretAccessKey = "<COS_HMAC_SECRET_ACCESS_KEY>";
// Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints
String cosServiceEndpoint = "https://s3.<region>.cloud-object-storage.appdomain.cloud";

AmazonS3 cosClient = AmazonS3ClientBuilder.standard()
                     .withEndpointConfiguration(new EndpointConfiguration(cosServiceEndpoint, "us-east-1"))
                     .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretAccessKey))).withPathStyleAccessEnabled(true)
                     .build();

GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, keyName)
                                                          .withMethod(httpMethod)
                                                          .withExpiration(expiration);

URL signedUrl = cosClient.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(signedUrl);

建立預先簽署的 URL 以上傳物件

Python 範例

import ibm_boto3
import os

bucket_name = '<bucket name>'
key_name = '<object key name>'
http_method = 'put_object'
expiration = 600  # time in seconds, default:600

access_key = os.environ.get('COS_HMAC_ACCESS_KEY_ID')
secret_key = os.environ.get('COS_HMAC_SECRET_ACCESS_KEY')
# Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints
cos_service_endpoint = 'https://s3.<region>.cloud-object-storage.appdomain.cloud'

cos = ibm_boto3.client("s3",
                       aws_access_key_id=access_key,
                       aws_secret_access_key=secret_key,
                       endpoint_url=cos_service_endpoint
                       )

signedUrl = cos.generate_presigned_url(http_method, Params={
                                       'Bucket': bucket_name, 'Key': key_name}, ExpiresIn=expiration)
print("presigned upload URL =>" + signedUrl)