使用java将文件上传到S3

时间:2015-10-26 14:31:46

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

我想使用maven java项目将文件上传到S3存储桶。

void uploadFile(String filePath,String fileName) throws IOException{
    String bucketName="my-feeds/prod/myfiles/myData";
    String currentDir = System.getProperty("user.dir");
    input = new FileInputStream(currentDir+"/src/main/resources/keyDetails.properties");
    prop.load(input);
    String YourAccessKeyID = prop.getProperty("accessKey");
    String YourSecretAccessKey = prop.getProperty("secretKey");
    AWSCredentials credentials = new BasicAWSCredentials(YourAccessKeyID, YourSecretAccessKey);
    AmazonS3 s3client = new AmazonS3Client(credentials);
    s3client.putObject(bucketName, fileName, new File(filePath));
}

但我得到例外:

Exception in thread "main" java.lang.NoSuchMethodError: com.amazonaws.AmazonWebServiceRequest.copyPrivateRequestParameters()Ljava/util/Map;

可能是什么原因?请帮忙。

我的pom.xml将此作为依赖:

<dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-s3</artifactId>
                <version>1.9.0</version>
            </dependency>

现在获得此异常

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. (Service: Amazon S3; Status Code: 301; Error Code: PermanentRedirect; " 

我更新版本后

2 个答案:

答案 0 :(得分:1)

存储桶名称应该只是my-feedsprod/myfiles/myData部分是密钥的一部分,而不是桶。

答案 1 :(得分:0)

您的存储桶名称无效。字符串my-feeds/prod/myfiles/myData看起来像您尝试创建的目录结构,而不是S3存储桶的名称。

进入S3控制台,找到您创建的S3存储桶的名称,并将其用作存储桶名称。 S3没有目录,只有对象,所以要模拟一个目录结构,你需要放置那些&#34;目录名称&#34;在对象键中。换句话说,传递给putObject()的第二个参数需要看起来像:"/my-feeds/prod/myfiles/myData/" + filename