使用预签名 URL 上传对象(适用于 Java 的 AWS 开发工具包) - Amazon Simple Storage Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本指南不再进行更新。有关当前信息和说明,请参阅新的 Amazon S3 用户指南

使用预签名 URL 上传对象(适用于 Java 的 AWS 开发工具包)

您可以使用适用于 Java 的 AWS 开发工具包生成一个预签名 URL,您或者从您这里获得了该 URL 的任何人可使用该 URL 将对象上传到 Amazon S3。当使用该 URL 上传对象时,Amazon S3 将在指定存储桶中创建对象。如果存储桶中已存在具有预签名 URL 中指定的相同键的对象,则 Amazon S3 会将现有对象替换为上传的对象。要成功完成上传,必须执行以下操作:

  • 在创建 GeneratePresignedUrlRequestHttpURLConnection 对象时指定 HTTP PUT 谓词。

  • 在完成上传后以某种方式与 HttpURLConnection 对象交互。以下示例使用 HttpURLConnection 对象检查 HTTP 响应代码,从而达到此目的。

有关预签名 URL 的更多信息,请参阅使用预签名 URL 上传对象

此示例将生成预签名 URL 并使用它将示例数据作为对象上传。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 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 com.amazonaws.services.s3.model.S3Object; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class GeneratePresignedUrlAndUploadObject { public static void main(String[] args) throws IOException { Regions clientRegion = Regions.DEFAULT_REGION; String bucketName = "*** Bucket name ***"; String objectKey = "*** Object key ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Set the pre-signed URL to expire after one hour. java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60; expiration.setTime(expTimeMillis); // Generate the pre-signed URL. System.out.println("Generating pre-signed URL."); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey) .withMethod(HttpMethod.PUT) .withExpiration(expiration); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); // Create the connection and use it to upload the new object using the pre-signed URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write("This text uploaded as an object via presigned URL."); out.close(); // Check the HTTP response code. To complete the upload and make the object available, // you must interact with the connection object in some way. connection.getResponseCode(); System.out.println("HTTP response code: " + connection.getResponseCode()); // Check to make sure that the object was uploaded successfully. S3Object object = s3Client.getObject(bucketName, objectKey); System.out.println("Object " + object.getKey() + " created in bucket " + object.getBucketName()); } 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(); } } }