2

我正在使用以下代码将文件上传到 S3 存储桶:

//upload code start
AWSCredentials credential = new BasicAWSCredentials(AppConstants.AWS_ACCESS_KEY_ID, AppConstants.AWS_SECRET_ACCESS_ID);
TransferManager manager = new TransferManager(credential);
// Transfer a file to an S3 bucket.
Upload upload = manager.upload("bucket_name", AppConstants.AWS_ACCESS_KEY_ID, file);
while (!upload.isDone()) {
    Thread.sleep(200);
}
//upload code end

我想将其放入存储桶内名为“android_uploads”的目录中,但传输管理器正在将所有文件上传到存储桶的根目录。

知道我怎么能做到这一点吗?

4

3 回答 3

8

您对 upload() 的调用不正确。它应该是这样的:

Upload upload = manager.upload("bucket_name", "android_uploads/" + fileName, file);
于 2014-08-01T17:46:20.923 回答
1
Upload upload = manager.upload("bucket_name","filePath where to be stored in bucket sub folder or directly to the bucket", file);
于 2015-02-10T14:45:01.250 回答
1

您应该能够通过简单的代码将任何文件类型上传到 Amazon s3,如下所示:

PutObjectRequest por = new PutObjectRequest(
                                BUCKET_NAME, Key,
                                file);

por.setCannedAcl(CannedAccessControlList.PublicReadWrite);
                        s3Client.putObject(por);

我为图像和文本文件上传尝试了此代码。

于 2015-04-11T13:00:16.527 回答