com.amazonaws.services.s3.model.PutObjectRequest的PutObjectRequest.putObject()方法抛出com.amazonaws.services.s3.model.AmazonS3Exception

时间:2016-01-27 16:55:42

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

嘿我正在尝试将图像上传到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
            }
        }
    }
}

在此代码中,putObject()抛出com.amazonaws.services.s3.model.PutObjectRequest抛出com.amazonaws.services.s3.model.AmazonS3Exception

在日志中显示

com.amazonaws.services.s3.model.AmazonS3Exception:状态代码:403,AWS服务:Amazon S3,AWS请求ID:8C576CDFEC92534C,AWS错误代码:AccessDenied,AWS错误消息:拒绝访问,S3扩展请求ID: q7Rs1xrUbnJgptnJKS5T5qhPc6Y3NT66qaGGA5or6as0i0JloLAYODiHcoztD + seWyExpVHM4ls =

1 个答案:

答案 0 :(得分:0)

403错误通常表示您的AWS凭据存在问题。您使用AmazonS3对象使用了哪些凭据?默认情况下,它们位于~/.aws/credentials

您可以使用aws命令行工具验证凭据是否正确。如果您之前没有使用它,请运行aws configure并输入您的信用卡。然后,您可以尝试使用aws s3 cp somefile s3://mybucket上传文件。

如果成功,您可以将Java SDK日志记录设置为documented here以获取更多信息。