我目前正在制作一个简单的React本机应用程序。因此,我从图库中挑选了一些图像,然后使用AWS Lambda函数(Java)将其上传到S3存储桶中。但是每当我尝试将这些图像上传到s3存储桶时,我都会不断收到没有此类文件或目录异常,并将其保存为0字节大小的文本文件格式。
package com.amazonaws.lambda.demo;
import java.io.File;
import java.io.IOException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
public class LambdaFunctionHandler implements RequestHandler<String, PutObjectResult> {
@SuppressWarnings("deprecation")
public PutObjectResult UploadFile(String imageURI) throws IOException {
PutObjectResult resultObj = new PutObjectResult();
AWSCredentials credentials = new BasicAWSCredentials("Ak", "Sk");
//String clientRegion = "region";
String bucketName = "bucket_name";
String fileObjKeyName = "sample_image_pic.jpg";
try {
AmazonS3 s3Client = new AmazonS3Client(credentials);
// Upload a file as a new object with ContentType and title specified.
resultObj = s3Client.putObject(new PutObjectRequest(bucketName, fileObjKeyName, new File(imageURI)));
}
catch(AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
}
catch(SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
return resultObj;
}
@Override
public PutObjectResult handleRequest(String imageURI, Context context) {
PutObjectResult result = null;
try {
result = UploadFile(imageURI);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
错误消息
{
"contentMd5": "1B2M2Y8AsgTpgAmY7PhCfg==",
"metadata": {
"userMetadata": {},
"contentLength": 0,
"requesterCharged": false,
"ETag": "d41d8cd98f00b204e9800998ecf8427e",
"rawMetadata": {
"Content-Length": 0,
"ETag": "d41d8cd98f00b204e9800998ecf8427e"
},
"instanceLength": 0
},
"requesterCharged": false,
"ETag": "d41d8cd98f00b204e9800998ecf8427e"
}
输入网址-> http://media.santabanta.com/gallery/cricket/ab%20de%20villiers/ab-de-villiers-3-d.jpg
对于这些概念,我也很陌生,如果我做错了什么,请纠正我。
谢谢