【发布时间】:2017-04-01 13:29:54
【问题描述】:
我们如何检查对象是否已完全上传到 Amazon s3?我正在从 android 应用程序将图像上传到 s3,然后调用 lambda 函数。我想在调用 lambda 之前确保该对象已上传到 s3。目前大文件上传会产生问题。
【问题讨论】:
标签: amazon-s3
我们如何检查对象是否已完全上传到 Amazon s3?我正在从 android 应用程序将图像上传到 s3,然后调用 lambda 函数。我想在调用 lambda 之前确保该对象已上传到 s3。目前大文件上传会产生问题。
【问题讨论】:
标签: amazon-s3
可以通过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 path) 中传递文件路径,它就会返回 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.equals(putObjectResult.getETag())) {
System.out.println("Congrats!! Document Uploaded Successfully");
}else {
System.out.println("Sorry! Could not upload the document");
}
希望对你有帮助,如果你有任何建议,请告诉我:)
【讨论】:
无需在将对象上传到 Amazon S3 后调用 AWS Lambda 函数,只需配置 S3 存储桶以触发 lambda 本身:
(来源:amazon.com)
这样,只有当对象成功上传到 S3 时才会触发该功能,对您的应用程序的工作量较少。
见:Using AWS Lambda with Amazon S3
如果你真的想单独做,那么你可以使用ETag(这是一个MD5校验和)来确认文件已按预期上传。
【讨论】: