1

電子メールの添付ファイルとして送信するために、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 に設定しています。

4

1 に答える 1

0

動作しました。電子メールの添付ファイルとして使用する前に、OutputStream を一時ファイルに保存するだけで済みました。参考までに以下にコードを掲載します。

Storage.Objects.Get getObject = storage.objects().get("bucket", "object");

OutputStream out = new FileOutputStream("/tmp/object");
// If you're not in AppEngine, download the whole thing in one request, if possible.
getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
getObject.executeMediaAndDownloadTo(out);
于 2016-02-16T21:21:03.797 に答える