我们如何检查对象是否已完全上传到Amazon s3?我正在从Android应用程序上传图像到s3,然后调用lambda函数。我想确保在调用lambda之前已将对象上传到s3。目前,大型文件上传会产生问题。
答案 0 :(得分:0)
不是在将对象上传到Amazon S3后调用AWS Lambda函数,只需配置S3存储桶以触发lambda本身:
这样,只有当对象成功上传到S3时才会触发该功能,并且您的应用程序的工作量会减少。
请参阅:Using AWS Lambda with Amazon S3
如果您确实想单独执行此操作,则可以使用ETag
(MD5校验和)确认文件已按预期上传。
答案 1 :(得分:0)
您可以使用 PutObjectResult 进行检查。响应表明对象已成功存储。 Amazon S3从不存储部分对象:如果您收到成功的响应,那么您可以确信存储了整个对象。所以主要的关键播放器是MD5算法,使用它可以确认你想要上传的对象是否与上传的相同,为了让你知道任何文件的MD5值,让我们说你可以使用它的图像以下找到如此
` package aws.example.s3;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5demo {
public static String MD5check(String path){
// TODO Auto-generated method stub
//Create checksum for this file
File file = new File(path);
//Get the checksum
String checksum="null";
try {
//Use MD5 algorithm
MessageDigest md5Digest;
try {
md5Digest = MessageDigest.getInstance("MD5");
checksum = getFileChecksum(md5Digest, file);
//see checksum
//System.out.println("Checksum: "+checksum);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return checksum;
}
private static String getFileChecksum(MessageDigest digest, File file)
throws IOException
{
//Get file input stream for reading the file content
FileInputStream fis = new FileInputStream(file);
//Create byte array to read data in chunks
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//Read file data and update in message digest
while ((bytesCount = fis.read(byteArray)) != -1) {
digest.update(byteArray, 0, bytesCount);
};
//close the stream; We don't need it now.
fis.close();
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100,
16).substring(1));
}
//return complete hash
return sb.toString();
}
}`
所以,您只需要在MD5check(String路径)中传递文件路径,它将返回MD5值
现在,在您编写上传任何文档的代码的课程中,只需使用以下代码
即可PutObjectResult putObjectResult = null;
try {
putObjectResult = s3.putObject(new PutObjectRequest(<Enter your Bucket name here>, <KeyName> , <pass your file object here>));
String MD5=MD5check(<pls enter your file path here>); //you can pass your file also directly, for that please change the method implementation of MD5Demo class accepting a file instead of path
System.out.println("Etag: "+putObjectResult.getETag()); //getting the Etag which is nothing but same as MD5 value of the file that got uploaded in S3
System.out.println("MD5: "+MD5);
if(MD5==putObjectResult.getETag()) {
System.out.println("Congrats!! Document Uploaded Successfully");
}else {
System.out.println("Sorry! Could not upload the document");
}
希望它有所帮助,如果您有任何建议,请告诉我们。)