Pab*_*blo 15 java amazon amazon-s3
尝试从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();
}
}
Run Code Online (Sandbox Code Playgroud)
这与amazon 在此处提供的基本相同,并且在尝试此其他版本时会出现完全相同的消息("配置文件不能为空")的相同异常.该问题与文件不存在或为空无关(我已经检查过一千种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)
Run Code Online (Sandbox Code Playgroud)
我的S3策略允许为所有类型的用户获取和放置对象.
关于发生了什么的任何想法?
emr*_*can 18
ProfileCredentialsProvider()创建一个新的配置文件凭据提供程序,该提供程序返回为默认配置文件配置的AWS安全凭证.
因此,如果您没有对默认配置文件进行任何配置~/.aws/credentials,则在尝试放置对象时,会产生该错误.
如果您在Lambda服务上运行代码,它将不提供此文件.在这种情况下,您也不需要提供凭据.只需将正确的IAM角色分配给lambda函数,然后使用默认构造函数来解决问题.
您可能希望根据需要更改TransferManager构造函数.
解决方案非常简单:我尝试在没有 Spring 的 AmazonS3 bean 的情况下实现此通信。
此链接将帮助您进行配置:
http://codeomissed.com/upload-file-to-s3-with-spring/