2016-11-30 171 views
7

尝试从我的Java Spring应用程序上传文件到Amazon S3时出现异常。方法很简单:AWS Java 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版本时完全相同的消息(“配置文件不能为空”)相同的异常出现。 该问题与文件不存在或为空无关(我已经用千种方法检查了在调用它之前存在的TransferManager.upload方法存在的File参数)。

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

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政策允许获取和puttings对象为所有类型的用户。

想知道发生了什么?

回答

9

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

所以,如果你还没有在~/.aws/credentials默认的配置文件的任何配置,同时试图把物体,它产生的错误。

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

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

+1

谢谢。 'Lambda'中同样的事情是,我将'ProfileCredentialsProvider'传递给导致该错误的构造函数。使用'AmazonS3 s3Client =新的AmazonS3Client();'工作。 –

+1

仅供参考 - 我在Docker ECS容器中遇到此问题。这与Lambda一样。在你的代码中查找'新的ProfileCredentialsProvider(someProfile))'并用'DefaultAWSCredentialsProviderChain.getInstance()'替换它' –

+0

我在这里留下了一个代码示例的答案:http://stackoverflow.com/questions/41796355/aws-error-下载对象,从-S3-轮廓文件不能-是空/ 44079772#44079772 –