Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用AWS SDK for Java生成预签名对象 URL

以下任务将引导您使用 Java 类来生成预签名 URL。

下载对象

1

创建 AmazonS3 类的实例。有关提供凭证的信息,请参阅使用 AWS SDK for Java。生成预签名 URL 时,这些凭证将用于为身份验证创建一个签名。

2

执行 AmazonS3.generatePresignedUrl 方法来生成预签名 URL。

通过创建 GeneratePresignedUrlRequest 类的实例来提供信息,包括存储桶名称、对象键和过期日期。默认情况下,请求将动作设置为 GET。要将预签名 URL 用于其他操作 (例如 PUT),您必须在创建请求时显式设置 verb

以下 Java 代码示例演示了上述任务。

Copy
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); java.util.Date expiration = new java.util.Date(); long msec = expiration.getTime(); msec += 1000 * 60 * 60; // 1 hour. expiration.setTime(msec); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey); generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default. generatePresignedUrlRequest.setExpiration(expiration); URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);

以下 Java 代码示例将生成预签名 URL,您可以将其授予其他用户,以便他们可以检索对象。您可以使用生成的预签名 URL 检索对象。要将预签名 URL 用于其他操作 (如放置对象),您必须在 verb 中显式设置 GetPreSignedUrlRequest。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.IOException; import java.net.URL; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.HttpMethod; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; public class GeneratePreSignedUrl { private static String bucketName = "*** Provide a bucket name ***"; private static String objectKey = "*** Provide an object key ***"; public static void main(String[] args) throws IOException { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { System.out.println("Generating pre-signed URL."); java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 60; // Add 1 hour. expiration.setTime(milliSeconds); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey); generatePresignedUrlRequest.setMethod(HttpMethod.GET); generatePresignedUrlRequest.setExpiration(expiration); URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest); System.out.println("Pre-Signed URL = " + url.toString()); } catch (AmazonServiceException exception) { System.out.println("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); System.out.println("Error Message: " + exception.getMessage()); System.out.println("HTTP Code: " + exception.getStatusCode()); System.out.println("AWS Error Code:" + exception.getErrorCode()); System.out.println("Error Type: " + exception.getErrorType()); System.out.println("Request ID: " + exception.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, " + "which means the client encountered " + "an internal error while trying to communicate" + " with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }