24

AzureBlobStorageに特定のファイルが存在することを確認したい。ファイル名を指定して確認することはできますか?FileNotFoundエラーが発生するたびに。

4

9 に答える 9

18

この拡張方法は次のことに役立ちます。

public static class BlobExtensions
{
    public static bool Exists(this CloudBlob blob)
    {
        try
        {
            blob.FetchAttributes();
            return true;
        }
        catch (StorageClientException e)
        {
            if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
            {
                return false;
            }
            else
            {
                throw;
            }
        }
    }
}

使用法:

static void Main(string[] args)
{
    var blob = CloudStorageAccount.DevelopmentStorageAccount
        .CreateCloudBlobClient().GetBlobReference(args[0]);
    // or CloudStorageAccount.Parse("<your connection string>")

    if (blob.Exists())
    {
        Console.WriteLine("The blob exists!");
    }
    else
    {
        Console.WriteLine("The blob doesn't exist.");
    }
}

http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob

于 2012-06-14T12:13:14.493 に答える
14

更新された SDK では、CloudBlobReference を取得したら、参照に対して Exists() を呼び出すことができます。

アップデート

関連ドキュメントはhttps://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblob.exists?view=azurestorage-8.1.3#Microsoft_WindowsAzure_Storage_Blob_CloudBlob_Exists_Microsoft_WindowsAzure_Storage_Blob_BlobRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_に移動されました。

WindowsAzure.Storage v2.0.6.1 を使用した私の実装

    private CloudBlockBlob GetBlobReference(string filePath, bool createContainerIfMissing = true)
    {
        CloudBlobClient client = _account.CreateCloudBlobClient();
        CloudBlobContainer container = client.GetContainerReference("my-container");

        if ( createContainerIfMissing && container.CreateIfNotExists())
        {
            //Public blobs allow for public access to the image via the URI
            //But first, make sure the blob exists
            container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        }

        CloudBlockBlob blob = container.GetBlockBlobReference(filePath);

        return blob;
    }

    public bool Exists(String filepath)
    {
        var blob = GetBlobReference(filepath, false);
        return blob.Exists();
    }
于 2013-08-05T22:15:49.663 に答える
4

ExistsAsyncCloudBlockBlobのメソッドを使用します。

bool blobExists = await cloudBlobContainer.GetBlockBlobReference("<name of blob>").ExistsAsync();
于 2018-07-16T17:58:10.980 に答える
2

Microsoft.WindowsAzure.Storage.Blob バージョン 4.3.0.0を使用すると、次のコードが機能するはずです (このアセンブリの古いバージョンには多くの重大な変更があります)。

コンテナー/ブロブ名と指定された API を使用します (Microsoft が実際にこれを実装したようです):

return _blobClient.GetContainerReference(containerName).GetBlockBlobReference(blobName).Exists();

BLOB URI を使用する (回避策):

  try 
  {
      CloudBlockBlob cb = (CloudBlockBlob) _blobClient.GetBlobReferenceFromServer(new Uri(url));
      cb.FetchAttributes();
  }
  catch (StorageException se)
  {
      if (se.Message.Contains("404") || se.Message.Contains("Not Found"))
      {
          return false;
      }
   }
   return true;

(ブロブが存在しない場合、属性のフェッチは失敗します。汚い、私は知っています:)

于 2015-05-15T21:44:29.460 に答える