電子メールの添付ファイルとして送信するために、Gcloud ストレージからダウンロードしたファイルを Mandrill API の添付ファイルとして使用しています。問題は、テキスト ファイルに対してのみ機能することですが、画像または Pdf の場合、添付ファイルが破損しています。 次のコードは、ファイルをダウンロードして Base64 でエンコードされた文字列に変換するためのものです。
Storage.Objects.Get getObject = getService().objects().get(bucket, object);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// If you're not in AppEngine, download the whole thing in one request, if possible.
getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
getObject.executeMediaAndDownloadTo(out);
//log.info("Output: {}", out.toString("UTF-8"));
return Base64.encodeBase64URLSafeString(out.toString("UTF-8")
.getBytes(StandardCharsets.UTF_8));
この文字列を Mandrill API の MessageContent の Content に設定しています。