biz-Streamマニュアル Webサービスサーバ ガイド 第12章 サンプルプログラム(.NET) 12.13 PDFを生成しAmazon S3へ格納するサンプルプログラム(WCF)

12.13 PDFを生成しAmazon S3へ格納するサンプルプログラム(WCF)

概要

PDFを生成しAmazon S3へ格納するサンプルプログラムです。


環境準備

事前に『2.7 クライアントアプリケーションの環境構築』を確認してください。


サンプル構成

項目 ファイルパス
ソースサンプル <bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample13.aspx.cs サンプル1

ソースサンプル

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using BizStreamWebApplication.BizStreamServiceReference;

namespace BizStreamWebApplication
{
    public partial class WSS_WCF_Sample13 : System.Web.UI.Page
    {
        // Webサービスサーバのホスト名
        const string WSS_HOST_NAME = "wsssrv";

        // Webサービスサーバ上のサンプルディレクトリの場所
        const string WSS_BIZSTREAM_SAMPLE_DIR = "\\bs\\sample";

        // Webサービスのエンドポイントプリフィックス
        const string WSS_SERVICE_LOCATION = "http://" + WSS_HOST_NAME + ":8080/axis2/services";

        // BASIC認証のユーザ名
        const string USER_NAME = @"bizuser1";

        // BASIC認証のパスワード
        const string PASSWORD = @"bizuser1";

        // 監査ロギング用のユーザ定義
        const string USER_DEF = "AAA";

        // 入力レイアウト定義ファイル名
        const string LAYOUT_FILE = WSS_BIZSTREAM_SAMPLE_DIR + "\\xml\\Sample1.xml";

        // 格納するファイルのバケット内のパス (先頭に/は不要)
        const string TARGET_FILE_PATH = "Sample/WSS_WCF_Sample13.pdf";


        protected void Page_Load(object sender, EventArgs e)
        {
            // Amazon S3にPDFファイルを格納
            generateOutputResponse response = generate();

            // レスポンスより格納したファイルの情報を取得
            amazonS3 s3 = response.amazonS3;
            String region = s3.region;         // リージョン
            String bucketName = s3.bucketName; // バケット名
            String filePath = s3.filePath;     // ファイルパス

            // ブラウザに結果を表示
            Response.Clear();
            Response.ContentType = "text/html;charset=UTF-8";
            Response.Write("<HTML><HEAD><TITLE>WSS_WCF_Sample13</TITLE></HEAD><BODY>");
            Response.Write("正常終了しました。<BR>");
            Response.Write("リージョン: " + region + "<BR>");
            Response.Write("バケット名: " + bucketName + "<BR>");
            Response.Write("ファイルパス: " + filePath + "<BR>");
            Response.Write("</BODY></HTML>");
            Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        private generateOutputResponse generate()
        {
            // クライアントを生成
            bizstreamPortTypeClient client =
                new bizstreamPortTypeClient("BizstreamSOAP11port",
                    WSS_SERVICE_LOCATION + "/bizstream?UserDef=" + USER_DEF);

            // 認証の設定
            BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
            elements.Find<HttpTransportBindingElement>().AuthenticationScheme =
                System.Net.AuthenticationSchemes.Basic;
            client.ClientCredentials.UserName.UserName = USER_NAME;
            client.ClientCredentials.UserName.Password = PASSWORD;

            // MTOMを有効化
            elements.Remove<TextMessageEncodingBindingElement>();
            MtomMessageEncodingBindingElement mtom = new MtomMessageEncodingBindingElement();
            mtom.ReaderQuotas.MaxStringContentLength = 100000;
            elements.Insert(0, mtom);
            elements.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
            client.Endpoint.Binding = new CustomBinding(elements);

            // リクエストメッセージの作成
            generateOutputRequest request = new generateOutputRequest();

            // Amazon S3への格納設定
            // ・下記のようなAPI指定の他にcloud_storage.propertiesファイルでの指定が可能
            // ・特にアクセスキーとシークレットキーはセキュリティ上cloud_storage.propertiesファイルにて設定すること
            // ・APIとpropertiesファイルと両方指定している場合はAPI指定が優先される
            // ・ファイルパスはAPIでの指定が必須
            amazonS3 s3 = new amazonS3();
            // s3.accessKey = "xxx";                     // アクセスキー (API指定は非推奨)
            // s3.secretKey = "xxx";                     // シークレットキー (API指定は非推奨)
            // s3.serviceEndpoint = "s3.amazonaws.com";  // サービスエンドポイント
            // s3.region = "ap-northeast-1";             // リージョン
            // s3.bucketName = "BucketName";             // バケット名 (バケットは事前に作成しておくこと)
            // s3.proxyHost = "proxy";                   // プロキシホスト名
            // s3.proxyPort = 8080;                      // プロキシポート番号
            s3.filePath = TARGET_FILE_PATH;              // バケット内のファイルパス (必須)
            s3.tags = new tag[10];
            for (int i = 1; i <= 10; i++)
            {
                // タグは10個まで設定可能
                s3.tags[i - 1] = new tag();
                s3.tags[i - 1].key = "key" + i;          // タグのキー
                s3.tags[i - 1].value = "value" + i;      // タグの値
            }

            // 出力ファイルの種類を設定
            pdf pdfData = new pdf();
            pdfData.Item = s3;
            output outputData = new output();
            outputData.Item = pdfData;
            request.output = outputData;

            // レイアウト定義を設定
            request.layoutData = new layoutDefinition[1];
            request.layoutData[0] = new layoutDefinition();
            request.layoutData[0].uri = LAYOUT_FILE;

            // リクエストを送信
            return client.generateOutput(request);
        }
    }
}