簡體   English   中英

使用Java將文件上傳到S3

[英]Upload file to S3 using java

我想使用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; " 

更新版本后

存儲桶名稱應僅為my-feeds prod/myfiles/myData部分是密鑰的一部分,而不是存儲桶。

您的存儲桶名稱無效。 字符串my-feeds/prod/myfiles/myData看起來像您要創建的目錄結構,而不是S3存儲桶的名稱。

進入S3控制台,找到您創建的S3存儲桶的名稱,並將其用作存儲桶名稱。 S3沒有目錄,只有對象,因此要模擬目錄結構,需要將那些“目錄名稱”放入對象鍵中。 換句話說,您要傳遞給putObject()的第二個參數應類似於: "/my-feeds/prod/myfiles/myData/" + filename

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM