java - Amazon S3GeneratePresignedUrlRequest - 新文件

标签 java amazon-web-services amazon-s3 upload

我想将新文件上传到 Amazon s3 到我的测试存储桶。

这是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("test-bucket", filename);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

但是我不断得到:

“指定的键不存在。”为文件名变量。

如何使此代码适用于新文件?

最佳答案

从外观上看,GeneratePresignedUrlRequest 适用于 S3 中的现有对象。

public GeneratePresignedUrlRequest(String bucketName, String key)

Creates a new request for generating a pre-signed URL that can be used as part of an HTTP GET request to access the Amazon S3 object stored under the specified key in the specified bucket.
Parameters:
bucketName - The name of the bucket containing the desired Amazon S3 object.
key - The key under which the desired Amazon S3 object is stored.

您可以使用 AmazonS3Client 类中的 putObject 方法之一。

PutObjectResult putObject(PutObjectRequest putObjectRequest)
Uploads a new object to the specified Amazon S3 bucket.

PutObjectResult putObject(String bucketName, String key, File file)
Uploads the specified file to Amazon S3 under the specified bucket and key name.

PutObjectResult putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata)
Uploads the specified input stream and object metadata to Amazon S3 under the specified bucket and key name.

将对象放入 S3 后,您就可以使用 key 实例化GeneratePresignedUrlRequest 对象并获取 URL。

关于java - Amazon S3GeneratePresignedUrlRequest - 新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34599840/

相关文章:

amazon-web-services - 连接fargate的Cloudformation参数

amazon-web-services - lambda 更新函数代码中的写操作超时

java - S3 ListObjectsV2Request 排序参数

golang s3 使用 s3manager.downloader 下载到缓冲区

java - 制作游戏时敌人数量/速度会以一定的分数增量自动增加 - 如何逻辑地实现?

java - Java 中的 HTML 处理 : Convert HTML to other formats

java - 内存不足错误: Java heap space-ArrayLists Java

Java 将给定日期转换为日期格式,如 MM/dd/yyyy

mysql - AWS EB 上的 Rails 应用程序 : Migrations fail

mysql - 将 mysql 转储发送到亚马逊 S3