[英]PutObjectRequest.putObject() method of com.amazonaws.services.s3.model.PutObjectRequest throws com.amazonaws.services.s3.model.AmazonS3Exception
Hey I am trying to upload an image to s3 bucket my code is: 嘿,我尝试将图像上传到s3存储桶,我的代码是:
public static void addObjectToBucketFromStream(AmazonS3 conn,
String bucketName, String keyName, InputStream inputStream,
Map<String, String> metadataMap,String contentType) throws IOException {
try {
ObjectMetadata metadata = new ObjectMetadata();
if (metadataMap != null) {
for (Iterator iterator = metadataMap.keySet().iterator(); iterator
.hasNext();) {
String key = (String) iterator.next();
String value = metadataMap.get(key);
metadata.addUserMetadata(key, value);
}
}
metadata.setContentLength(inputStream.available());
if (contentType!=null && !"".equals(contentType)){
metadata.setContentType(contentType);
}
PutObjectRequest por = new PutObjectRequest(bucketName, keyName,
inputStream, metadata);
por.setCannedAcl(CannedAccessControlList.PublicRead);
conn.putObject(por);
int a;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}
}
In this code putObject() throws com.amazonaws.services.s3.model.PutObjectRequest throws com.amazonaws.services.s3.model.AmazonS3Exception 在此代码中,putObject()抛出com.amazonaws.services.s3.model.PutObjectRequest抛出com.amazonaws.services.s3.model.AmazonS3Exception
In log it is showing 在日志中显示
com.amazonaws.services.s3.model.AmazonS3Exception: Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 8C576CDFEC92534C, AWS Error Code: AccessDenied, AWS Error Message: Access Denied, S3 Extended Request ID: q7Rs1xrUbnJgptnJKS5T5qhPc6Y3NT66qaGGA5or6as0i0JloLAYODiHcoztD+seWyExpVHM4ls= com.amazonaws.services.s3.model.AmazonS3Exception:状态代码:403,AWS服务:Amazon S3,AWS请求ID:8C576CDFEC92534C,AWS错误代码:AccessDenied,AWS错误消息:访问被拒绝,S3扩展请求ID:q7Rs1xrUbnJgptiJKS5T5qhaa6t4a5t5qhpc6y4a5t5qhpc6y1a =
A 403 error usually indicates a problem with your AWS credentials. 403错误通常表示您的AWS凭证存在问题。 What credentials are you using with your
AmazonS3
object? 您在
AmazonS3
对象中使用什么凭证? By default, they'll be located in ~/.aws/credentials
. 默认情况下,它们将位于
~/.aws/credentials
。
You can verify credentials are correct with the aws
command-line tool. 您可以使用
aws
命令行工具验证凭据是否正确。 If you haven't used it before, run aws configure
and enter your creds. 如果您以前从未使用过它,请运行
aws configure
并输入您的凭据。 Then you can attempt to upload a file with aws s3 cp somefile s3://mybucket
. 然后,您可以尝试使用
aws s3 cp somefile s3://mybucket
上传文件。
If that succeeds, you can enable the Java SDK logging as documented here to get more information. 如果成功,则可以启用此处记录的Java SDK日志以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.