使用預先簽章的 URL 來共用物件 - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用預先簽章的 URL 來共用物件

若要授予對存放在本機 Outpost 上物件的有限時間存取權限,而不會更新儲存貯體政策,您可以使用預先簽章 URL。使用預先簽章 URL,身為儲存貯體擁有者的您可以與虛擬私有雲端 (VPC) 中的個人共享物件,或授予他們上傳或刪除物件的能力。

當您使用 AWS SDK 或 AWS Command Line Interface (AWS CLI) 建立預先簽署的 URL 時,您可以將 URL 與特定動作建立關聯。您也可以選擇自訂到期時間 (最低 1 秒,最高 7 天) 來授予預先簽章 URL 有限時間的存取權。當您共用預先簽章 URL 時,VPC 中的個人可以執行內嵌在 URL 中的動作,如同原始簽章使用者一樣。當 URL 到達到期時間時,該 URL 就會過期且再也無法運作。

當您建立預先簽章 URL 時,必須提供安全憑證,然後指定下列項目:

  • 適用於 Amazon S3 on Outposts 儲存貯體的存取點 Amazon Resource Name (ARN)

  • 物件索引鍵

  • HTTP 方法 (GET 用於下載物件)

  • 過期日期和時間

預先簽章 URL 僅在指定的期間內有效。也就是說,您必須在到期日期和時間之前開始 URL 所允許的操作。您可以多次使用預先簽章 URL,直到到期日期和時間為止。如果使用暫時字符建立了預先簽章的 URL,那麼 URL 會在字符過期時過期,即使您使用較晚的過期時間建立 URL 亦然。

虛擬私有雲端 (VPC) 中可存取預先簽章 URL 的使用者可以存取物件。例如,若儲存貯體中有一段影片且儲存貯體與物件皆為私有,即可透過產生預先簽章的 URL 來與其他人分享這段影片。由於預先簽章 URL 會將 S3 on Outposts 儲存貯體的存取權授予擁有 URL 的任何人,因此我們建議您妥善保護這些 URL。如需有關保護預先簽署 URL 的詳細資訊,請參閱限制預先簽章的 URL 功能

任何具備有效安全憑證的使用者,均可建立預先簽章的 URL。然而,只有具備許可執行作為預先簽章 URL 基礎操作的人員,才能建立預先簽章 URL。如需詳細資訊,請參閱 誰可以建立預先簽章的 URL

您可以使用 AWS SDK 與 AWS CLI來產生預先簽章 URL,以在 S3 on Outposts 儲存貯體中共享物件。如需詳細資訊,請參閱下列範例。

您可以使用 AWS SDK 產生可提供給其他人的預先簽署 URL,以便他們擷取物件。

注意

當您使用 AWS SDK 產生預先簽署的 URL 時,預先簽署 URL 的到期時間上限為建立後 7 天。

Java

以下範例會產生預先簽章的 URL,您可以將其提供給其他人,讓他們可以從 S3 on Outposts 儲存貯體擷取物件。如需詳細資訊,請參閱 使用適用於 S3 on OutOutposts 的預先簽章 URL。若要使用此範例,請以您自己的資訊取代 user input placeholders

如需建立和測試工作範例的指示,請參閱 AWS SDK for Java 發人員指南中的入門指南。

import com.amazonaws.AmazonServiceException; import com.amazonaws.HttpMethod; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import java.io.IOException; import java.net.URL; import java.time.Instant; public class GeneratePresignedURL { public static void main(String[] args) throws IOException { Regions clientRegion = Regions.DEFAULT_REGION; String accessPointArn = "*** access point ARN ***"; String objectKey = "*** object key ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); // Set the presigned URL to expire after one hour. java.util.Date expiration = new java.util.Date(); long expTimeMillis = Instant.now().toEpochMilli(); expTimeMillis += 1000 * 60 * 60; expiration.setTime(expTimeMillis); // Generate the presigned URL. System.out.println("Generating pre-signed URL."); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(accessPointArn, objectKey) .withMethod(HttpMethod.GET) .withExpiration(expiration); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); System.out.println("Pre-Signed URL: " + url.toString()); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }
.NET

以下範例會產生預先簽章的 URL,您可以將其提供給其他人,讓他們可以從 S3 on Outposts 儲存貯體擷取物件。如需詳細資訊,請參閱 使用適用於 S3 on OutOutposts 的預先簽章 URL。若要使用此範例,請以您自己的資訊取代 user input placeholders

如需有關設定和執行程式碼範例的詳細資訊,請參閱 .NET 開發人員指南中的AWS SDK for .NET門。 AWS

using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; namespace Amazon.DocSamples.S3 { class GenPresignedURLTest { private const string accessPointArn = "*** access point ARN ***"; private const string objectKey = "*** object key ***"; // Specify how long the presigned URL lasts, in hours. private const double timeoutDuration = 12; // Specify your bucket Region (an example Region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); string urlString = GeneratePreSignedURL(timeoutDuration); } static string GeneratePreSignedURL(double duration) { string urlString = ""; try { GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest { BucketName = accessPointArn, Key = objectKey, Expires = DateTime.UtcNow.AddHours(duration) }; urlString = s3Client.GetPreSignedURL(request1); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } return urlString; } } }
Python

下列範例使用了 SDK for Python (Boto3) 來產生預先簽章的 URL 以共用物件。例如,使用 Boto3 用戶端和 generate_presigned_url 函數來產生允許您 GET 物件的預先簽章的 URL。

import boto3 url = boto3.client('s3').generate_presigned_url( ClientMethod='get_object', Params={'Bucket': 'ACCESS_POINT_ARN', 'Key': 'OBJECT_KEY'}, ExpiresIn=3600)

如需有關使用 SDK for Python (Boto3) 產生預先簽章的 URL 的詳細資訊,請參閱《AWS SDK for Python (Boto) API 參考》中的「Python」。

下列範例 AWS CLI 命令會為 Outposts 儲存貯體上的 S3 產生預先簽署的 URL。若要使用此範例,請以您自己的資訊取代 user input placeholders

注意

使用產生預先簽署的 URL 時,預先簽署 URL 的到期時間上限為建立後 7 天。 AWS CLI

aws s3 presign s3://arn:aws:s3-outposts:us-east-1:111122223333:outpost/op-01ac5d28a6a232904/accesspoint/example-outpost-access-point/mydoc.txt --expires-in 604800

如需詳細資訊,請參閱《AWS CLI 命令參考》中的 presign