我想检查Azure Blob存储中是否存在特定文件。是否可以通过指定文件名进行检查?每次我都找不到文件错误。
答案 0 :(得分:26)
var blob = client.GetContainerReference(containerName).GetBlockBlobReference(blobFileName);
if (blob.Exists())
//do your stuff
答案 1 :(得分:15)
此扩展方法可以帮助您:
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
答案 2 :(得分:8)
使用更新的SDK,一旦您拥有CloudBlobReference,您就可以在引用上调用Exists()。
<强>更新强>
我使用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();
}
答案 3 :(得分:3)
使用CloudBlockBlob的➜ bitmagic git:(master) ✗ time ./next-1
./next-1 4.27s user 0.01s system 99% cpu 4.313 total
➜ bitmagic git:(master) ✗ time ./next-2
./next-2 12.42s user 0.00s system 99% cpu 12.436 total
方法。
ExistsAsync
答案 4 :(得分:2)
使用 Microsoft.WindowsAzure.Storage.Blob版本4.3.0.0 ,以下代码应该可以使用(对于此程序集的旧版本,有很多重大更改):
使用容器/ blob名称和给定的API(现在看来微软已经实现了这个):
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;
(如果blob不存在,则获取属性将失败。很脏,我知道:)。
答案 5 :(得分:0)
使用新软件包Azure.Storage.Blobs
BlobServiceClient blobServiceClient = new BlobServiceClient("YourStorageConnectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("YourContainerName");
BlobClient blobClient = containerClient.GetBlobClient("YourFileName");
然后检查是否存在
if (blobClient.Exists()){
//your code
}
答案 6 :(得分:0)
对于最新版本的SDK,您需要使用ExistsAsync
方法,
public async Task<bool> FileExists(string fileName)
{
return await directory.GetBlockBlobReference(fileName).ExistsAsync();
}
这里是code sample。
答案 7 :(得分:0)
## dbutils.widgets.get to call the key-value from data bricks job
storage_account_name= dbutils.widgets.get("storage_account_name")
container_name= dbutils.widgets.get("container_name")
transcripts_path_intent= dbutils.widgets.get("transcripts_path_intent")
# Read azure blob access key from dbutils
storage_account_access_key = dbutils.secrets.get(scope = "inteliserve-blob-storage-secret-scope", key = "storage-account-key")
from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name=storage_account_name, account_key=storage_account_access_key)
def blob_exists():
container_name2 = container_name
blob_name = transcripts_path_intent
exists=(block_blob_service.exists(container_name2, blob_name))
return exists
blobstat = blob_exists()
print(blobstat)