Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用预签名 URL 上传对象 (AWS SDK for Java)

以下任务将引导您使用 Java 类来上传使用预签名 URL 的对象。

上传对象

1

创建 AmazonS3 类的实例。

2

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

您可以通过创建 GeneratePresignedUrlRequest 类的实例来提供存储桶名称、对象键和过期日期。如果您想要使用此 URL 来上传对象,您必须在创建它时指定 HTTP 动作 PUT。

3

任何拥有预签名 URL 的人都可以上传对象。

上传将创建对象或将任何现有的对象替换为预签名 URL 中指定的相同键值。

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

AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); java.util.Date expiration = new java.util.Date(); long msec = expiration.getTime(); msec += 1000 * 60 * 60; // Add 1 hour. expiration.setTime(msec); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey); generatePresignedUrlRequest.setMethod(HttpMethod.PUT); generatePresignedUrlRequest.setExpiration(expiration); URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest); // Use the pre-signed URL to upload an object.

下面的 Java 示例代码生成一个预签名 URL。然后该示例代码使用此预签名 URL 将示例数据作为对象进行上传。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

import java.io.IOException; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; 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 GeneratePresignedUrlAndUploadObject { private static String bucketName = "*** bucket name ***"; private static String objectKey = "*** 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.PUT); generatePresignedUrlRequest.setExpiration(expiration); URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest); UploadObject(url); 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()); } } public static void UploadObject(URL url) throws IOException { HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream()); out.write("This text uploaded as object."); out.close(); int responseCode = connection.getResponseCode(); System.out.println("Service returned response code " + responseCode); } }