检查 Azure 存储中是否存在 blob
我有一个非常简单的问题(我希望如此!) - 我只想查明某个 blob(具有我定义的名称)是否存在于特定容器中。如果它确实存在,我会下载它,如果不存在,我会做其他事情。
我在管间做了一些搜索,显然曾经有一个名为DoesExist或类似的函数...但是与许多Azure API一样,这个函数似乎不再存在(或者如果存在,也有一个非常巧妙地伪装的名字)。
I've got a very simple question (I hope!) - I just want to find out if a blob (with a name I've defined) exists in a particular container. I'll be downloading it if it does exist, and if it doesn't then I'll do something else.
I've done some searching on the intertubes and apparently there used to be a function called DoesExist or something similar... but as with so many of the Azure APIs, this no longer seems to be there (or if it is, has a very cleverly disguised name).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
新的 API 具有 .Exists() 函数调用。只需确保您使用
GetBlockBlobReference
,它不会执行对服务器的调用。它使该功能变得简单:The new API has the .Exists() function call. Just make sure that you use the
GetBlockBlobReference
, which doesn't perform the call to the server. It makes the function as easy as:注意:这个答案现在已经过时了。请参阅 Richard 的答案,了解检查是否存在的简单方法
不,您并没有错过一些简单的东西...我们在新的 StorageClient 库中隐藏此方法方面做得很好。 :)
我刚刚写了一篇博客文章来回答您的问题: http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob。
简短的答案是:使用 CloudBlob.FetchAttributes(),它对 blob 执行 HEAD 请求。
Note: This answer is out of date now. Please see Richard's answer for an easy way to check for existence
No, you're not missing something simple... we did a good job of hiding this method in the new StorageClient library. :)
I just wrote a blog post to answer your question: http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob.
The short answer is: use CloudBlob.FetchAttributes(), which does a HEAD request against the blob.
看起来很蹩脚,您需要捕获异常来测试 blob 是否存在。
Seem lame that you need to catch an exception to test it the blob exists.
如果 blob 是公共的,您当然可以从无数知道如何执行此操作的语言/环境/平台中发送一个 HTTP HEAD 请求,然后检查响应。
核心 Azure API 是基于 RESTful XML 的 HTTP 接口。 StorageClient 库是它们周围许多可能的包装器之一。这是 Sriram Krishnan 用 Python 完成的另一个操作:
http: //www.sriramkrishnan.com/blog/2008/11/python-wrapper-for-windows-azure.html
它还展示了如何在 HTTP 级别进行身份验证。
我在 C# 中为自己做了类似的事情,因为我更喜欢通过 HTTP/REST 的视角而不是通过 StorageClient 库的视角来看待 Azure。很长一段时间我什至都懒得去实现 ExistsBlob 方法。我的所有 blob 都是公共的,执行 HTTP HEAD 很简单。
If the blob is public you can, of course, just send an HTTP HEAD request -- from any of the zillions of languages/environments/platforms that know how do that -- and check the response.
The core Azure APIs are RESTful XML-based HTTP interfaces. The StorageClient library is one of many possible wrappers around them. Here's another that Sriram Krishnan did in Python:
http://www.sriramkrishnan.com/blog/2008/11/python-wrapper-for-windows-azure.html
It also shows how to authenticate at the HTTP level.
I've done a similar thing for myself in C#, because I prefer to see Azure through the lens of HTTP/REST rather than through the lens of the StorageClient library. For quite a while I hadn't even bothered to implement an ExistsBlob method. All my blobs were public, and it was trivial to do HTTP HEAD.
如果您不喜欢其他解决方案,这里有一个不同的解决方案:
我使用的是 Azure.Storage.Blobs NuGet 包 12.4.1 版本。
我得到一个 >Azure.Pageable 对象,它是容器中所有 Blob 的列表。然后,我使用 BlobItem 来检查容器内每个 Blob 的 Name 属性是否等于 Name 属性。强>LINQ。 (当然,如果一切都有效)
希望这对将来的人有帮助。
Here's a different solution if you don't like the other solutions:
I am using version 12.4.1 of the Azure.Storage.Blobs NuGet Package.
I get an Azure.Pageable object which is a list of all of the blobs in a container. I then check if the name of the BlobItem equals to the Name property of each blob inside the container utilizing LINQ. (If everything is valid, of course)
Hopefully this helps someone in the future.
新的 Windows Azure 存储库已包含 Exist() 方法。
它位于 Microsoft.WindowsAzure.Storage.dll 中。
以 NuGet 包形式提供
创建者:微软
ID:WindowsAzure.Storage
版本:2.0.5.1
另请参阅 msdn< /a>
The new Windows Azure Storage Library already contains the Exist() method.
It´s in the Microsoft.WindowsAzure.Storage.dll.
Available as NuGet Package
Created by: Microsoft
Id: WindowsAzure.Storage
Version: 2.0.5.1
See also msdn
我就是这样做的。为有需要的人展示完整代码。
This is the way I do it. Showing full code for those who need it.
如果您的 blob 是公共的并且您只需要元数据:
If your blob is public and you need just metadata:
使用 Azure Blob 存储库 v12,您可以使用
BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()
回答了另一个类似问题:https://stackoverflow.com/a/63293998/4865541
With Azure Blob storage library v12, you can use
BlobBaseClient.Exists()/BlobBaseClient.ExistsAsync()
Answered on another similar question: https://stackoverflow.com/a/63293998/4865541
相同的Java版本(使用新的v12 SDK)
这使用共享密钥凭证授权(帐户访问密钥)
Java version for the same ( using the new v12 SDK )
This uses the Shared Key Credential authorization (account access key)
如果您不喜欢使用异常方法,那么 judell 建议的基本 C# 版本如下。但请注意,您确实也应该处理其他可能的响应。
If you don't like using the exception method then the basic c# version of what judell suggests is below. Beware though that you really ought to handle other possible responses too.
使用更新的 SDK,一旦您拥有 CloudBlobReference,您就可以对您的引用调用 Exists()。
请参阅 http://msdn.microsoft。 com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.exists.aspx
With the updated SDK, once you have the CloudBlobReference you can call Exists() on your reference.
See http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.exists.aspx
尽管这里的大多数答案在技术上都是正确的,但大多数代码示例都在进行同步/阻塞调用。除非您受到非常旧的平台或代码库的约束,否则 HTTP 调用应始终异步完成,并且 SDK 在这种情况下完全支持它。只需使用
ExistsAsync()
而不是Exists()
。Although most answers here are technically correct, most code samples are making synchronous/blocking calls. Unless you're bound by a very old platform or code base, HTTP calls should always be done asynchonously, and the SDK fully supports it in this case. Just use
ExistsAsync()
instead ofExists()
.