JavaEar 专注于收集分享传播有价值的技术资料

How to get the MD5 of an Object that has already been stored?

I have lots of Objects already stored in GCS. Now I want to get their MD5 hashes that GCS has already calculated, from my Google App Engine application using the Google Cloud Storage Client Library, but I can't find anything that exposes it.

I know that the ETag contains the MD5 in the XML api for backwards compatibility, but it says don't rely on that being there for any other APIs.

I know that I can get to them with the JSON api, but I don't want to write up all that HTTPClient and Auth code and JSON parsing to get to a single attribute.

I know I can also calculate the MD5 at upload time but these files are already uploaded, and reading them all again to just calculate the MD5 seems wasteful.

1个回答

    最佳答案
  1. The MD5 hash is not available for every object. While most objects have an MD5 hash, many objects do not. For example, objects that have been created using object composition will often not have a recorded MD5 hash.

    The XML API and the JSON API both expose the MD5 hash, where one is available. If you don't want to mess with HTTP calls, you could use the Google API client for Java to speak to the GCS JSON API. Here are instructions for setting it up on App Engine: https://code.google.com/p/google-api-java-client/wiki/GoogleAppEngine#Getting_Started

    You can use the AppIdentityCredential class to use the credentials of your app to access Google Cloud Storage objects, and when you do, the MD5 is in the "md5Hash" property of the object. Please note that it is a base64 encoding rather than a hexadecimal encoding.