Amazon web services 为什么amazon aws jdk 1.11.95在将图像上载到aws S3时返回文件未找到异常?
我正在尝试将图像上载到AWS S3 该web应用程序在tomcat服务器的本地桌面上运行。 当我从服务器上传图像时,我看到http请求多部分文件中的文件详细信息,我能够得到它的大小和详细信息 这就是我建立连接的方式Amazon web services 为什么amazon aws jdk 1.11.95在将图像上载到aws S3时返回文件未找到异常?,amazon-web-services,amazon-s3,aws-sdk,Amazon Web Services,Amazon S3,Aws Sdk,我正在尝试将图像上载到AWS S3 该web应用程序在tomcat服务器的本地桌面上运行。 当我从服务器上传图像时,我看到http请求多部分文件中的文件详细信息,我能够得到它的大小和详细信息 这就是我建立连接的方式 File convFile = new File( file.getOriginalFilename()); file.transferTo(convFile); AmazonS3 s3 = AmazonS3ClientBuilder.stand
File convFile = new File( file.getOriginalFilename());
file.transferTo(convFile);
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_WEST_2) //regionName is a string for a region not supported by the SDK yet
.withCredentials(new AWSStaticCredentialsProvider
(new BasicAWSCredentials("key", "accessId")))
// .setEndpointConfiguration(new EndpointConfiguration("https://s3.console.aws.amazon.com", "us-west-1"))
.enablePathStyleAccess()
.disableChunkedEncoding()
.build();
s3.putObject(new PutObjectRequest(bucketName, "key", convFile));
我尝试了两种方法
1) 将多部分文件转换为java.io.file并上载
Error: com.amazonaws.SdkClientException: Unable to calculate MD5 hash: MyImage.png (No such file or directory)
2) 以ByTestStream的形式发送图像
Error: I am getting java.io.FileNotFound Exception: /path/to/tomcat/MyImage.tmp not found
实际图像名称为MyImage.png
无论我尝试哪种方法,我都会遇到异常 好的。有几个问题
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
byte[] contentBytes = null;
try {
InputStream is = file.getInputStream();
contentBytes = IOUtils.toByteArray(is);
} catch (IOException e) {
System.err.printf("Failed while reading bytes from %s", e.getMessage());
}
Long contentLength = Long.valueOf(contentBytes.length);
objectMetadata.setContentLength(contentLength);
objectMetadata.setHeader("filename", fileNameWithExtn);
/*
* Reobtain the tmp uploaded file as input stream
*/
InputStream inputStream = file.getInputStream();
File convFile = new File(fileNameWithExtn); //If i don't do //this, I think I was getting file not found or MD5 error.
file.transferTo(convFile);
FileUtils.copyInputStreamToFile(inputStream, convFile); //you //need to have commons.io in your pom.xml for this FileUtils to work. Not //the apache FileUtils.
AmazonS3 s3 = new AmazonS3Client(new AWSStaticCredentialsProvider
(new BasicAWSCredentials("<yourkeyId>", "<YourAccessKey>")));
s3.setRegion(Region.US_West.toAWSRegion());
s3.setEndpoint("yourRegion.amazonaws.com");
versionId = s3.putObject(new PutObjectRequest("YourBucketName", name, convFile)).getVersionId();
ObjectMetadata ObjectMetadata=new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
byte[]contentBytes=null;
试一试{
InputStream=file.getInputStream();
contentBytes=IOUtils.toByteArray(is);
}捕获(IOE异常){
System.err.printf(“从%s读取字节时失败”,e.getMessage());
}
Long contentLength=Long.valueOf(contentBytes.length);
objectMetadata.setContentLength(contentLength);
setHeader(“filename”,fileNameWithExtn);
/*
*重新获取tmp上传文件作为输入流
*/
InputStream InputStream=file.getInputStream();
File convFile=新文件(fileNameWithExtn)//如果我不这么做,我想我是因为找不到文件或MD5错误。
文件。传输到(convFile);
copyInputStreamToFile(inputStream,convFile)//您//需要在pom.xml中包含commons.io,此文件utils才能工作。不是//apache FileUtils。
AmazonS3 s3=新AmazonS3客户端(新AWSStaticCredentialsProvider
(新的基本凭证(“,”);
s3.设置区域(Region.US_West.toAWSRegion());
s3.setEndpoint(“yourRegion.amazonaws.com”);
versionId=s3.putObject(新的PutObjectRequest(“YourBucketName”,name,convFile)).getVersionId();
您能不能不简单地将putObject()与文件对象一起使用,如@jarmod AmazonS3Client所示,在提到的aws jdk中不推荐使用。对于这个明显的问题,我深表歉意,但是本地文件是否存在于指定的位置且可读?如果您从图片中删除AWS SDK,并尝试从简单Java应用程序中打开相同的文件,那么它是否有效?