我使用以下代码将文件上传到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"的目录中。在我的存储桶中,但传输管理器正在将所有文件上传到存储桶的根目录。
知道如何实现这一目标吗?
答案 0 :(得分:6)
您对upload()的调用不正确。应该是这样的:
Upload upload = manager.upload("bucket_name", "android_uploads/" + fileName, file);
答案 1 :(得分:1)
Upload upload = manager.upload("bucket_name","filePath where to be stored in bucket sub folder or directly to the bucket", file);
答案 2 :(得分:1)
您应该可以通过简单的代码将任何文件类型上传到Amazon s3,如下所示:
PutObjectRequest por = new PutObjectRequest(
BUCKET_NAME, Key,
file);
por.setCannedAcl(CannedAccessControlList.PublicReadWrite);
s3Client.putObject(por);
我尝试使用此代码进行图像和文本文件上传。