2016-11-17 110 views

回答

0

而不是上传对象到Amazon S3后调用一个AWS LAMBDA功能,只需配置S3存储触发拉姆达本身:

S3 to Lambda

这样,功能才会被触发时该对象已成功上传到S3,并且您的应用程序的工作量较少。

参见:Using AWS Lambda with Amazon S3

(哦,如果你真的想分开做,那么你可以使用ETag,其中MD5校验,确认为预计该文件已被上传)

+0

这种方式将有麻烦发送结果回到手机。这就是为什么我使用RequestResponse方法来调用lambda。 – JSmith

+0

如何使用电子标签进行操作? – JSmith

+0

请参见:[如何获取Amazon S3上文件的md5sum](http://stackoverflow.com/questions/1775816/how-to-get-the-md5sum-of-a-file-on-amazons-s3 ) –

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(字符串路径)的文件路径,它将返回的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"); 
      } 

H操作它有帮助,请让我知道如果您有任何建议:)

相关问题