エクスポート (0) 印刷
すべて展開

SharedAccessPolicy クラス

開始日時、有効期限、および共有アクセス署名のアクセス許可を指定する共有アクセス ポリシーを表します。

名前空間: Microsoft.WindowsAzure.StorageClient
アセンブリ: Microsoft.WindowsAzure.StorageClient (Microsoft.WindowsAzure.StorageClient 内)

'使用
Dim instance As SharedAccessPolicy

public class SharedAccessPolicy
public class SharedAccessPolicy
public class SharedAccessPolicy

BLOB の共有アクセス署名を作成するコード例を次に示します。 共有アクセス ポリシーは署名自体に対して作成されます。

static void CreateSAS()
{
    //Retrieve storage account information from an app.config file.
    //This is one way to store and retrieve a connection string if you are writing an application 
    //that will run locally, rather than in Windows Azure.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageAccountConnectionString"]);

    //Create the blob client object.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    //Get a reference to the container for which the shared access signature will be created.
    CloudBlobContainer container = blobClient.GetContainerReference("mysascontainer");
    container.CreateIfNotExist();

    //Create a permission policy, consisting of a shared access policy and a public access setting, and store it on the container. 
    BlobContainerPermissions blobPermissions = new BlobContainerPermissions();

    //The public access setting explicitly specifies that the container is private, so that it can't be accessed anonymously.
    blobPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

    //Set the permission policy on the container.
    container.SetPermissions(blobPermissions);

    // Get the shared access signature to share with users, specifying a signature-level access policy.
    string sas = container.GetSharedAccessSignature(new SharedAccessPolicy()
    {
        SharedAccessStartTime = DateTime.Now,
        SharedAccessExpiryTime = DateTime.Now.AddHours(10),
        Permissions = SharedAccessPermissions.Write | SharedAccessPermissions.Read
    });

    //The shared access signature then can be used to create a service client. 
    //This code would likely be run from a different client, but is included here to 
    //demonstrate how to consume the shared access signature.

    //Create the blob client directly, using the shared access signature
    CloudBlobClient sasBlobClient = new CloudBlobClient(storageAccount.BlobEndpoint,
        new StorageCredentialsSharedAccessSignature(sas));

    //Return a reference to a blob.
    CloudBlob blob = sasBlobClient.GetBlobReference("mysascontainer/myblob.txt");

    //Upload text to the blob. If the blob does not yet exist, it will be created. If the blob does exist, its existing
    //content will be overwritten.
    blob.UploadText("Write to a blob using shared access credentials.");
}


コンテナー レベルで共有アクセス ポリシーを作成した後、ポリシーと関連付けられた共有アクセス署名を作成する例を次に示します。

static void CreateSASUsingContainerAccessPolicy()
{
    //Retrieve storage account information from an app.config file.
    //This is one way to store and retrieve a connection string if you are writing an application 
    //that will run locally, rather than in Windows Azure.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageAccountConnectionString"]);
    //Create the blob client object.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    //Get a reference to the container for which shared access signature will be created.
    CloudBlobContainer container = blobClient.GetContainerReference("mysascontainer");
    container.CreateIfNotExist();

    //Create a permission policy, consisting of a container-level access policy and a public access setting, and store it on the container. 
    BlobContainerPermissions blobPermissions = new BlobContainerPermissions();
    //The container-level access policy provides read/write access to the container for 10 hours.
    blobPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy()
    {
        SharedAccessStartTime = DateTime.Now,
        SharedAccessExpiryTime = DateTime.Now.AddHours(10),
        Permissions = SharedAccessPermissions.Write | SharedAccessPermissions.Read
    });

    //The public access setting explicitly specifies that the container is private, so that it can't be accessed anonymously.
    blobPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

    //Set the permission policy on the container.
    container.SetPermissions(blobPermissions);

    //Get the shared access signature to share with clients.
    //Note that this call passes in an empty access policy, so that the shared access signature will use the 
    //'mypolicy' access policy that's defined on the container.
    string sas = container.GetSharedAccessSignature(new SharedAccessPolicy(), "mypolicy");

    // Clients can use the signature to create a service client.
    StorageCredentialsSharedAccessSignature sasCreds = new StorageCredentialsSharedAccessSignature(sas);
    CloudBlobClient sasBlobClient = new CloudBlobClient(storageAccount.BlobEndpoint,
        new StorageCredentialsSharedAccessSignature(sas));
    //Return a reference to a blob.
    CloudBlob blob = sasBlobClient.GetBlobReference("mysascontainer/myblob.txt");
    //Upload text to the blob. If the blob does not yet exist, it will be created. If the blob does exist, its existing
    //content will be overwritten.
    blob.UploadText("Hello SAS World");
}


コンテナー レベルの共有アクセス ポリシーでは、コンテナーまたはコンテナーの BLOB の共有アクセス署名に適用できる一連のパラメーターが定義されています。 SharedAccessPolicy オブジェクトで表されるアクセス ポリシーでは、開始日時、有効期限、および共有アクセスに対する一連のアクセス許可が定義されています。

共有アクセス署名自体に対して単一の SharedAccessPolicy オブジェクトを定義することもできます。 コンテナーには、SharedAccessPolicies クラスによって表される共有アクセス ポリシーのコレクションを設定できます。


System.Object
  Microsoft.WindowsAzure.StorageClient.SharedAccessPolicy

この型の public static (Visual Basic では Shared) メンバーは、スレッド セーフです。インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

開発プラットフォーム

Windows XP Home Edition, Windows XP Professional, Windows Server 2003、Windows Server 2008、および Windows 2000

対象プラットフォーム

コミュニティの追加

表示:
© 2014 Microsoft