2016-11-17 27 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操作它有幫助,請讓我知道如果您有任何建議:)

相關問題