我已经解决了这个问题,就像 smarx 提到的那样。在上传之前,我计算文件的 md5-Hash 并在 blob 的属性中更新它:
import java.security.MessageDigest
import com.microsoft.windowsazure.services.core.storage.utils.Base64;
import com.google.common.io.Files
String putFile(String remoteFolder, String filePath){
File fileReference = new File (filePath)
// the user is already authentificated and the container is not null
CloudBlockBlob blob = container.getBlockBlobReference(remoteFolderName+"/"+filePath);
FileInputStream fis = new FileInputStream(fileReference)
if(blob){
BlobProperties props = blob.getProperties()
MessageDigest md5digest = MessageDigest.getInstance("MD5")
String md5 = Base64.encode(Files.getDigest(fileReference, md5digest))
props.setContentMD5(md5)
blob.setProperties(props)
blob.upload(fis, fileReference.length())
return fileReference.getName()
}else{
//ErrorHandling
return ""
}
}
文件上传后,我可以使用以下方法获取 ContentMD5:
String getHash(String remoteFolderName, String filePath) {
String fileName = new File(filePath).getName()
CloudBlockBlob blob = container.getBlockBlobReference(remoteFolderName+"/"+filePath)
if(!blob) return ""
blob.downloadAttributes()
byte[] hash = Base64.decode(blob.getProperties().getContentMD5())
BigInteger bigInt = new BigInteger(1, hash)
return bigInt.toString(16).padLeft(32, '0')
}