我有一个非常简单的问题(希望如此!)- 我想找出是否在特定容器中存在一个由我定义名称的 blob。 如果它存在,我将下载它,如果不存在,我将执行其他操作。
我在互联网上进行了一些搜索,显然曾经有一个名为 DoesExist 或类似名称的函数...但是就像许多 Azure API 一样,它似乎已经消失了(或者如果存在,那它有一个非常巧妙的伪装名称)。
我有一个非常简单的问题(希望如此!)- 我想找出是否在特定容器中存在一个由我定义名称的 blob。 如果它存在,我将下载它,如果不存在,我将执行其他操作。
我在互联网上进行了一些搜索,显然曾经有一个名为 DoesExist 或类似名称的函数...但是就像许多 Azure API 一样,它似乎已经消失了(或者如果存在,那它有一个非常巧妙的伪装名称)。
GetBlockBlobReference
,该函数不会执行到服务器的调用。这使得函数变得非常简单:public static bool BlobExistsOnCloud(CloudBlobClient client,
string containerName, string key)
{
return client.GetContainerReference(containerName)
.GetBlockBlobReference(key)
.Exists();
}
OpenRead
的函数,它不会抛出异常或返回空流等任何内容。只有当你开始从中下载时才会出现错误。将所有这些处理放在一个地方要容易得多 :) - porges似乎很糟糕,需要捕获异常来测试blob是否存在。
public static bool Exists(this CloudBlob blob)
{
try
{
blob.FetchAttributes();
return true;
}
catch (StorageClientException e)
{
if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
{
return false;
}
else
{
throw;
}
}
}
http://www.sriramkrishnan.com/blog/2008/11/python-wrapper-for-windows-azure.html
它还展示了如何在HTTP层面进行身份验证。如果您不喜欢其他解决方案,这里有一个不同的解决方案:
我使用的是Azure.Storage.Blobs NuGet包的12.4.1版本。
我得到了一个Azure.Pageable对象,它是容器中所有blob的列表。然后我使用LINQ检查每个blob的Name属性是否等于BlobItem的名称。(当然,如果一切都有效)
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Linq;
using System.Text.RegularExpressions;
public class AzureBlobStorage
{
private BlobServiceClient _blobServiceClient;
public AzureBlobStorage(string connectionString)
{
this.ConnectionString = connectionString;
_blobServiceClient = new BlobServiceClient(this.ConnectionString);
}
public bool IsContainerNameValid(string name)
{
return Regex.IsMatch(name, "^[a-z0-9](?!.*--)[a-z0-9-]{1,61}[a-z0-9]$", RegexOptions.Singleline | RegexOptions.CultureInvariant);
}
public bool ContainerExists(string name)
{
return (IsContainerNameValid(name) ? _blobServiceClient.GetBlobContainerClient(name).Exists() : false);
}
public Azure.Pageable<BlobItem> GetBlobs(string containerName, string prefix = null)
{
try
{
return (ContainerExists(containerName) ?
_blobServiceClient.GetBlobContainerClient(containerName).GetBlobs(BlobTraits.All, BlobStates.All, prefix, default(System.Threading.CancellationToken))
: null);
}
catch
{
throw;
}
}
public bool BlobExists(string containerName, string blobName)
{
try
{
return (from b in GetBlobs(containerName)
where b.Name == blobName
select b).FirstOrDefault() != null;
}
catch
{
throw;
}
}
}
希望这能帮助未来的某个人。
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("ContainerName");
// Retrieve reference to a blob named "test.csv"
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.csv");
if (blockBlob.Exists())
{
//Do your logic here.
}
BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()
方法。 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
string code = "";
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
code = response.StatusCode.ToString();
}
catch
{
}
return code; // if "OK" blob exists
如果您不喜欢使用异常方法,那么 judell 建议的基本 c# 版本如下。但请注意,您真的应该处理其他可能的响应。
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Method = "HEAD";
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
if (myResp.StatusCode == HttpStatusCode.OK)
{
return true;
}
else
{
return false;
}