がんふぃーるど室長の不定期ブログ

ただいま助手と悪戦苦闘中!

  ホーム :: 連絡をする :: 同期する  :: Login
投稿数  90  :: 記事 7 :: コメント 15113 :: トラックバック 13

ニュース


自己紹介

名前:がんふぃーるど
肩書:室長
種別:人間・男
資格一覧:
MCP 70-215 Installing, Configurating, and Administering Microsoft Windows 2000 Server
MCTS .NET Framework 2.0 - Distributed Applications
MCTS .NET Framework 2.0 - Web Applications

犬紹介


名前:なうら
肩書:助手
種別:犬・狆・メス
誕生日:2006/7/9
特技:鼻水飛ばし、甘噛、奇襲・急襲・強襲、そそう、お手、お座り、待て

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

DIME Attachments S3 Bucket Browser 拡張

S3で1MB以上のデータを転送するためには、DIME という仕様を使う必要があります。AmazonのResourceCenterにあるサンプルS3 Bucket BrowserではDIMEを使用してデータのやり取りを行っていないので、今回はそこを拡張しようと思います。

まず、.NET FrameworkのWebサービスではDIMEを標準ではサポートしていないので、DIME Attachmentを使用するため、WSEをインストールします。ただし、最新のWSEではDIME Attachmentの部分をMTOMという仕様で置き換えてしまっているので、今回はWSE 1.0(注)を使用します。

注:WSE 2.0を使用してS3にアクセスしようとすると少々問題があるので(WSEの中のWS-Addressingの実装を強制され、サーバ側でエラーが発生する)、WSE 1.0を使用しています。

WSE 1.0 SP1 インストール

WSE 1.0 SP1 をダウンロードし、インストールします。

WSE 1.0 を参照に加える

Microsoft.Web.Serviceを参照に加えます。

コードの変更

Webサービスプロキシの親クラスをMicrosoft.Web.Services.WebServicesClientProtocolに変更

まず、Visual Studioの[ソリューションエクスプローラ]で[すべてのファイルを表示]するようにします。

そうすると、[Web References]のcom.amazonaws.s3を展開できるようになるので、Refence.csというファイルを開きます。これはwsdlで作成されたプロキシクラスです。

このReference.csの中にあるプロキシクラスの親クラスをSystem.Web.Services.Protocols.SoapHttpClientProtocolからMicrosoft.Web.Services.WebServicesClientProtocolに変更します。

public partial class AmazonS3 : Microsoft.Web.Services.WebServicesClientProtocol

S3コンストラクタ

プロキシクラスの設定を行います。

public S3(string sAccessKeyId, string sSecretKey)

{

m_sAccessKeyId = sAccessKeyId;

m_sSecretAccessKey = sSecretKey;

m_objS3.RequestSoapContext.Path.MustUnderstand = false; // WS-Routing は使用しないので、falseにする。

}

S3.PutObject(string bucketName, string keyName, string fileName, long contentLength, string contentType)メソッド追加

内容はPutObjectInlineからコピってきます。基本的にはPutObjectInlineと一緒ですが、DIME Attachmentにアップロードするファイルを追加しましょう。

m_objS3.RequestSoapContext.Attachments.Add(new Microsoft.Web.Services.Dime.DimeAttachment(contentType, Microsoft.Web.Services.Dime.TypeFormatEnum.MediaType, fileName));

objS3PutObjectResult = m_objS3.PutObject(bucketName, keyName, aobjMetaDataEntries, contentLength, null, 0, false, AccessKeyId, timeStamp, true, sSignature, null);

S3.GetObjectメソッド修正

Objectを取得するときは、Webメソッドの引数でDIMEかInlineか選択できます。なので、S3.GetObjectメソッドにbool isOver1MBObjectというObjectのサイズが1MBを超えているかどうか判断できる引数を追加します。Webサービスから取得したDIME AttachmentのStreamはそのままファイルに焼き付けたほうが効率がいいのですが、今回はGetObjectResult.Dataにバイト配列として格納してしまいます。(コードの修正が少なくて済むので…)

public GetObjectResult GetObject(string bucketName, string keyName, bool isOver1MBObject)

{

(中略)

try

{

objS3GetObjectResult = m_objS3.GetObject(bucketName, keyName, true, true, !isOver1MBObject, AccessKeyId, timeStamp, true, sSignature, null);

m_bSoapError = false;

m_sSoapErrorMessage = "";

if (isOver1MBObject)

{

Stream stream = m_objS3.ResponseSoapContext.Attachments[0].Stream;

byte[] buffer = new byte[(int)stream.Length];

stream.Read(buffer, 0, (int)stream.Length);

objS3GetObjectResult.Data = buffer;

}

}

(中略)

return objS3GetObjectResult;

}

Objectのサイズの取得

ファイルをアップロードしようとする場合はFileInfoなどで確認すれば良いのですが、ファイルをダウンロードしようとする場合は、listView1.Items[n].SubItems[3]から取得します。

コンパイルと実行

細々とした部分は省略しましたが、後はコンパイルして実行すれば1MB以上のデータをやりとりできるようになります。ちなみに、1MB以上のデータを画面右にドロップすると数秒間固まります(笑)。

今回はWSEを使用してDIME Attachmentを使用してファイルを送受信するようにS3 Bucket Browserを拡張しましたが、Webサービス関連は調べれば調べるほど恐ろしくなってきますね…まさに伏魔殿って感じです。最近の仕様もWSE関連は複雑化の一途を辿っている様に見えますし、もっと簡単にならないのかな…日本語のドキュメントももっと作って欲しいなーなんて思ってますw

# WSE 2.0 を使った場合についてですが、実は自分を中継サーバと勘違いさせ(m_objS3.Pipeline.IsIntermediary = true;)、パイプライン処理の最後でWS-Addressing等の情報を削除させることができます。これでいけることはいけるのですが、あまりにも強引な方法なので使わない方が良いと思います。それよりも、「Discussions in Web Services Enhancements」というマイクロソフトのニュースグループでWS-Addressingを無効にする話題に上っていて、パイプライン処理にカスタムクラスを差し込めばなんとかなりそうなことが書かれていました。ちょっとメールで問い合わせ中です。

投稿日時 : 2007年3月7日 0:37

コメント

# FSotdbkKbwonXWMRQP 2014/07/19 19:23 http://crorkz.com/
outFK1 Very informative blog. Want more.

# WyjxuxaeGHBIb 2014/09/09 20:17 http://www.arrasproperties.com/4009-e-3rd-st-apt-3
whoah this blog is magnificent i love studying your posts. Stay up the good paintings! You already know, many persons are looking around for this info, you can help them greatly.

# jdZxaLKbmffg 2015/05/03 13:15 horny
qQwZNb http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# QsXZvmoKdYsm 2015/05/19 13:12 Jose
I'm in a band http://www.smhv.nl/over-smhv about caverta 50 But at that moment, and for the first time in my life, I felt not like a tourist but like a traveller, even a pioneer. All too soon the group arrived and we moved off – more cliffs to scramble down, more paths to negotiate and, for me, another mule waiting at the bottom.


# OVWVFCazBCnQeURkpUv 2015/05/19 13:12 Jane
Withdraw cash http://www.aslan.ie/biography/ intagra tablets pills 50 mg Work crews over the summer completed a $400,000 renovation of a wing of Central Jr. High to accommodate the 300 or so Plaza Towers students. Banks of lockers were removed, extra classrooms were added with fresh carpet and Promethean interactive boards, and bathrooms were remodeled for smaller visitors, said Tammy Baker, Central's principal. On-site counselors will be monitoring not just students but parents and teachers as well, many of whom lost homes or relatives in the storm, she said. "It'll be emotional," Baker said.


# lEkoSbNKLB 2015/05/19 13:12 Freddy
What do you do? http://www.jmktrust.org/about/ acheter priligy 60 mg Several blokes were quick to jump onto the page and boast about their prowess at playing ‘Pull a Pig’. One squaddie boasted about the number of ‘pigs’ who used to frequent the camp, noting that “most fat girls were normally gagging for it!”


# imitation cartier bangle rose 2017/07/08 4:47 delfdpwmefsptrcvb@hotmal.com
Grow the fuck up. Its just a parody. I seriously doubt anyone is aiming for an award here. Enjoy it for what it is, and stop hating.
imitation cartier bangle rose http://www.clovebangles.com/category/fake-cartier-love-bracelet/

# re: Internet Explorer 11 で、右クリックしたときに表示されるメニューで、Bingではなく、Google をデフォルトの検索エンジンとして設定するには? 2017/12/08 18:12 meadc
http://www.nfl-shop.co nfl store
http://www.nike-chaussures.fr/ Air Max TN Homme
http://www.culinar-hannover.de/ Nike Air Force
http://www.pradahandbags.co prada handbags
http://www.burberry-outletsale.us/ burberry outlet sale store
me adc12.8


コメントの投稿

タイトル:
名前:
Url:
コメント: