如何在Java中通过SDK上传文件到s3 bucket?[关闭]

wbgh16ku  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(187)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
3天前关闭。
Improve this question

  • 我想:通过Java上传文件到s3 bucket
  • 我的问题是:我得到一个错误

您之前创建命名存储桶的请求成功,您已经拥有了它。
我的问题是为什么在执行s3客户端的putObject方法时,我会得到一个与创建bucket相关的错误。为什么会这样?我该怎么解决这个问题?我已经花了好几天了快疯了。
这是我的代码:

private void putObjectFromRequestBody(final RequestBody requestBody,
                                      final Charset charset,
                                      final String bucketName,
                                      final String key) {

final PutObjectRequest putObjectRequest = this.buildPutObjectRequest(bucketName, key, charset);

this.s3Client.putObject(putObjectRequest, requestBody);
}
wnvonmuf

wnvonmuf1#

要将对象上传到Amazon S3存储桶,请确保您使用的是推荐的SDK版本,即AWS SDK for Java V2。您可以在AWS代码库中找到许多Java和S3操作的示例。
Upload an object to an Amazon S3 bucket using an AWS SDK
下面是完整的代码示例:

/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/
package com.example.s3;

// snippet-start:[s3.java2.s3_object_upload.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
// snippet-end:[s3.java2.s3_object_upload.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class PutObject {

    public static void main(String[] args) {
        final String usage = "\n" +
            "Usage:\n" +
            "  <bucketName> <objectKey> <objectPath> \n\n" +
            "Where:\n" +
            "  bucketName - The Amazon S3 bucket to upload an object into.\n" +
            "  objectKey - The object to upload (for example, book.pdf).\n" +
            "  objectPath - The path where the file is located (for example, C:/AWS/book2.pdf). \n\n" ;

        if (args.length != 3) {
            System.out.println(usage);
            System.exit(1);
        }

        String bucketName = args[0];
        String objectKey = args[1];
        String objectPath = args[2];
        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
            .region(region)
            .credentialsProvider(credentialsProvider)
            .build();

        putS3Object(s3, bucketName, objectKey, objectPath);
        s3.close();
    }

    // snippet-start:[s3.java2.s3_object_upload.main]
    // This example uses RequestBody.fromFile to avoid loading the whole file into memory.
    public static void putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {
        try {
            Map<String, String> metadata = new HashMap<>();
            metadata.put("x-amz-meta-myVal", "test");
            PutObjectRequest putOb = PutObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .metadata(metadata)
                .build();

            s3.putObject(putOb, RequestBody.fromFile(new File(objectPath)));
            System.out.println("Successfully placed " + objectKey +" into bucket "+bucketName);

        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
   // snippet-end:[s3.java2.s3_object_upload.main]
}

相关问题