【问题标题】:AWS Java S3 Uploading error: "profile file cannot be null"AWS Java S3 上传错误:“配置文件不能为空”
【发布时间】:2017-04-15 07:38:23
【问题描述】:

尝试从我的 Java Spring 应用程序将文件上传到 Amazon S3 时出现异常。方法很简单:

private void productionFileSaver(String keyName, File f) throws InterruptedException {

        String bucketName = "{my-bucket-name}";
        TransferManager tm = new TransferManager(new ProfileCredentialsProvider());        
        // TransferManager processes all transfers asynchronously, 
        // so this call will return immediately.
        Upload upload = tm.upload(
                bucketName, keyName, new File("/mypath/myfile.png"));

        try {
            // Or you can block and wait for the upload to finish
            upload.waitForCompletion();
            System.out.println("Upload complete.");
        } catch (AmazonClientException amazonClientException) {
            System.out.println("Unable to upload file, upload was aborted.");
            amazonClientException.printStackTrace();
        }
    }

亚马逊提供的here基本相同,在尝试this other版本时出现同样的异常,提示完全相同的信息(“profile file cannot be null”)。 该问题与不存在或为空的文件无关(我已经以一千种方式检查了 TransferManager.upload 方法收到的 File 参数在调用它之前是否存在)。

我找不到关于我的异常消息“profile file cannot be null”的任何信息。错误日志的第一行如下:

com.amazonaws.AmazonClientException: Unable to complete transfer: profile file cannot be null
    at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.unwrapExecutionException(AbstractTransfer.java:281)
    at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.rethrowExecutionException(AbstractTransfer.java:265)
    at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.waitForCompletion(AbstractTransfer.java:103)
    at com.fullteaching.backend.file.FileController.productionFileSaver(FileController.java:371)
    at com.fullteaching.backend.file.FileController.handlePictureUpload(FileController.java:247)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

我的 S3 策略允许为所有类型的用户获取和放置对象。

知道发生了什么吗?

【问题讨论】:

    标签: java amazon-s3 amazon


    【解决方案1】:

    ProfileCredentialsProvider() 创建一个新的配置文件凭证提供程序,它返回为默认配置文件配置的 AWS 安全凭证。

    因此,如果您在~/.aws/credentials 处没有任何默认配置文件配置,则在尝试放置对象时,会产生该错误。

    如果您在 Lambda 服务上运行代码,它不会提供此文件。在这种情况下,您也不需要提供凭据。只需将正确的 IAM 角色分配给您的 lambda 函数,然后使用默认构造函数即可解决问题。

    您可能想根据需要更改TransferManager 构造函数。

    【讨论】:

    • 谢谢。 Lambda 中的相同内容,我将 ProfileCredentialsProvider 传递给导致该错误的构造函数。使用AmazonS3 s3Client = new AmazonS3Client(); 有效。
    • 仅供参考 - 我在 docker ECS 容器中遇到了这个问题。这与 Lambda 相同。在您的代码中查找 new ProfileCredentialsProvider(someProfile)) 并将其替换为 DefaultAWSCredentialsProviderChain.getInstance()
    • 我在这里留下了代码示例的答案:stackoverflow.com/questions/41796355/…
    【解决方案2】:

    解决方案非常简单:我试图在没有用于 Spring 的 AmazonS3 bean 的情况下实现这种通信。

    此链接将有助于配置:

    http://codeomitted.com/upload-file-to-s3-with-spring/

    【讨论】:

      【解决方案3】:

      我的代码运行良好,如下所示:

      AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(DefaultAWSCredentialsProviderChain.getInstance()).withRegion(clientRegion).build();
      

      【讨论】:

      • 这对我不起作用。当我检查 DefaultAWSCredentialsProviderChain.getInstance() 时,正在创建一个可能导致问题的新 ProfileCredentialsProvider。不确定。还是没有解决
      猜你喜欢
      • 2023-03-14
      • 2017-06-07
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多