AWS Java S3 上传错误:“配置文件不能为空”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40897548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:32:51  来源:igfitidea点击:

AWS Java S3 Uploading error: "profile file cannot be null"

javaamazon-s3amazon

提问by Pablo

I get an exception when trying to upload a file to Amazon S3 from my Java Spring application. The method is pretty simple:

尝试从 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();
        }
    }

It is basically the same that amazon provides here, and the same exception with the exactly same message ("profile file cannot be null") appears when trying this otherversion. The problem is not related to the file not existing or being null (I have already checked in a thousand ways that the File argument recieved by TransferManager.uploadmethod exists before calling it).

与亚马逊在此处提供的基本相同,并且在尝试此其他版本时出现了具有完全相同消息(“配置文件不能为空”)的相同异常。问题与文件不存在或为空无关(我已经以一千种方式检查了方法接收的 File 参数TransferManager.upload在调用之前是否存在)。

I cannot find any info about my exception message "profile file cannot be null". The first lines of the error log are the following:

我找不到关于我的异常消息“配置文件不能为空”的任何信息。错误日志的第一行如下:

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)

My S3 policy allows getting and puttings objects for all kind of users.

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

Any idea about what's happening?

知道发生了什么吗?

回答by emre can

ProfileCredentialsProvider()creates a new profile credentials provider that returns the AWS security credentials configured for the default profile.

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

So, if you haven't any configuration for default profile at ~/.aws/credentials, while trying to put object, it yields that error.

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

If you run your code on Lambda service, it will not provide this file. In that case, you also do not need to provide credentials. Just assign right IAM Role to your lambda function, then using default constructor should solve issue.

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

You may want to change TransferManagerconstructor according to your needs.

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

回答by Pablo

The solution was pretty simple: I was trying to implement this communication without an AmazonS3 bean for Spring.

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

This link will help with the configuration:

此链接将有助于配置:

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

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

回答by m.nguyencntt

my code worked fine as below:

我的代码工作正常,如下所示:

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