GUID(または他の名前)でblobを保存することは可能ですか?ただし、ユーザーがファイルURIを要求した場合http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20- 37A03E504E3Fダウンロードは、MyText.txtなどのわかりやすい名前で表示されます。
6 に答える
ユーザーが Windows Azure でファイル (BLOB) をダウンロードできるようにするには、次の 4 つの方法があります。
直接ダウンロード– コンテナーのアクセスをパブリック読み取りアクセスまたは完全なパブリック読み取りアクセスに設定し、URL をエンド ユーザーに公開します。この方法の欠点は明らかにセキュリティです。URL が公開されている場合、アクセスを制御する方法がありません。ダウンロードを検出する方法も、ダウンロードの前後にコードを実行する方法もありません。
API ダウンロード– ユーザーは Windows アプリ、Silverlight などを実行する必要があります。また、アプリにはストア アカウント名とキーが含まれている必要があります。これにより、セキュリティが損なわれる可能性があります。特に、複数の顧客が同じアカウントを使用している場合 (もちろん、顧客は独自のコンテナーを持つことができます)。
プロキシ ダウンロード– ユーザーに YOUR サーバー上の URL にアクセスしてもらい、Azure からファイルを取得してユーザーに送信します。これの利点は、ダウンロードを完全に制御できること、ダウンロードの前後にコードを実行できること、Azure URL やアカウント情報を公開する必要がないことです。実際、エンド ユーザーは、ファイルが Azure に保存されていることさえわかりません。この方法でファイル名を上書きすることもできます。欠点は、すべてのトラフィックがサーバーを通過することです。そのため、ここでボトルネックが発生する可能性があります。
パススルー ダウンロード (Azure Shared Access Signature) - 署名を作成し、ユーザーが Azure にリダイレクトされる URL にこの署名を挿入します。署名により、ユーザーは限られた期間ファイルにアクセスできます。これはおそらく最良の選択肢です。ダウンロード前にカスタムコードを実行できるようにし、ユーザーの最大ダウンロード速度を確保し、適切なレベルのセキュリティも確保します.
ファイルをユーザーにストリーミングし、ファイル名をオーバーライドするコード スニペットを次に示します。
//Retrieve filenname from DB (based on fileid (Guid))
// *SNIP*
string filename = "some file name.txt";
//IE needs URL encoded filename. Important when there are spaces and other non-ansi chars in filename.
if (HttpContext.Current.Request.UserAgent != null && HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE"))
filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).Replace("+", " ");
context.Response.Charset = "UTF-8";
//Important to set buffer to false. IIS will download entire blob before passing it on to user if this is not set to false
context.Response.Buffer = false;
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
context.Response.AddHeader("Content-Length", "100122334"); //Set the length the file
context.Response.ContentType = "application/octet-stream";
context.Response.Flush();
//Use the Azure API to stream the blob to the user instantly.
// *SNIP*
fileBlob.DownloadToStream(context.Response.OutputStream);
詳細については、次のブログ投稿を参照してください: http://blog.degree.no/2012/04/downloading-blobs-from-windows-azure/
2013 年 11 月の時点で、Windows Azure Blob Storage で content-disposition ヘッダーがサポートされるようになりました。ヘッダーは、BLOB の作成時または共有アクセス署名を介して割り当てることができます。
ドキュメントの保存メソッドを変更して、ダウンロードするフレンドリ名であるオプションのパラメーターを取得しました。
public void SaveDocument(Stream documentToSave, string contentType, string containerName, string url, string friendlyName = null)
{
var container = GetContainer(containerName);
container.CreateIfNotExists();
var blob = container.GetBlockBlobReference(url);
blob.Properties.ContentType = contentType;
if (friendlyName != null)
blob.Properties.ContentDisposition = "attachment; filename=" + friendlyName;
blob.UploadFromStream(documentToSave);
}
詳細: http://blog.simontimms.com/2013/12/02/content-disposition-comes-to-azure/
答えはノーだ。BLOB に content-disposition ヘッダーを設定する必要がありますが、そのヘッダーを BLOB ストレージに設定する方法はありません。
まず、BLOB URI に独自のカスタム ドメインを使用できます: http://blogs.msdn.com/b/avkashchauhan/archive/2011/03/22/using-custom-domain-name-with-windows-azure-storage-代わりに、windows-ストレージ名-blob-core-windows-net.aspx
あなたのための解決策の例...
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(ConfigurationManager.AppSettings["WindowsAzureStorageAccountName"], ConfigurationManager.AppSettings["WindowsAzureStorageAccountKey"]);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credentials, true);
CloudBlobContainer blobContainer = blobClient.GetContainerReference("mycontainer");
// e.g. GUID of currently logged in user
string fileName = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();
CloudBlob blob = blobContainer.GetBlobReference(fileName);
byte[] blobArray = blob.DownloadByteArray();
Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment; filename=" + "MyText.txt");
Response.BinaryWrite(blobArray);
Response.End();