Amazon S3:`key`是上传到bucket而不是文件的对象

时间:2017-05-05 05:18:57

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

请检查以下代码

import java.io.File;
import java.io.IOException;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class UploadObjectSingleOperation {
    private static String bucketName     = "*******";
    private static String keyName        = "************";
    private static String uploadFileName = "C:/Users/Yohan/Desktop/asdasd.html";

    public static void main(String[] args) throws IOException {
        BasicAWSCredentials creds = new BasicAWSCredentials(keyName, "**********"); 
        AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).withRegion(Regions.AP_SOUTH_1).build();
//            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());
        }
    }
}

好的,我上面的代码正用于将文件上传到Amazon S3 Bucket。我的S3存储桶位于离我的客户端Asia Pacific - Mumbai最近的位置。

上面的代码工作正常,但我注意到以下内容。

  1. 上传的内容始终是key。真实文件未上传。请查看下图。
  2. enter image description here

    为什么会这样?当我使用S3的Web界面上传文件时,它完全正常。

1 个答案:

答案 0 :(得分:0)

发现错误。我的问题中的代码来自亚马逊教程 - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html

但我确定它不正确或已弃用。

注意以下行

s3client.putObject(new PutObjectRequest(bucketName, keyName, file));

要使其工作,而不是keyName,您必须传递带扩展名的文件名。例如websitepage.html