从Amazon S3获取请求实现的响应

时间:2015-06-04 05:19:34

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

这是使用Amazon S3上传对象的代码示例。

public class UploadObjectSingleOperation {
    private static String bucketName     = "*** Provide bucket name ***";
    private static String keyName        = "*** Provide key ***";
    private static String uploadFileName = "*** Provide file name ***";

    public static void main(String[] args) throws IOException {
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        try {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(uploadFileName);
            s3client.putObject(new PutObjectRequest(
                                     bucketName, keyName, file));

         } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " +
                    "means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " +
                    "means the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

在此,如果我们获得AmazonServiceException,我们可以获取HTTP状态代码。有没有办法获得成功响应的HTTP状态代码?我的目标是确认图像上传成功。

1 个答案:

答案 0 :(得分:0)

发现没有办法获得HTTPStatus代码以获得成功的响应。只需听取异常即可确保成功上传。