我试图检索容器中blob项的URL。使用Azure的instructions我已成功上传图像并将图像作为数据检索,但这在我的情况下并不理想。
在Azure Storage Explorer中,实际上有一个容器中每个blob项的URL,但我根本无法弄清楚如何在代码中获取它。有人能指出我正确的方向
答案 0 :(得分:2)
在Azure存储资源管理器中,容器中的每个blob项实际上都有一个URL,但我无法弄清楚如何在代码中获取它。
事实上,我不太清楚你想要的是列出blob url还是获取blob项目。对于它们两者,您可以使用Azure Storage Client Library for iOS来实现。
因此,如果您想在容器中 get the blob url ,则每次返回延续令牌时都可以使用辅助方法递归调用list blobs方法。
对于blob,基URI包括帐户名称,容器名称和blob名称:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(options)
{
_httpContextAccessor = httpContextAccessor;
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
{
var httpContext = _httpContextAccessor.HttpContext;
if(httpContext != null)
{
var authenticatedUserName = httpContext.User.Identity.Name;
// If it returns null, even when the user was authenticated, you may try to get the value of a specific claim
var authenticatedUserId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value
// var authenticatedUserId = _httpContextAccessor.HttpContext.User.FindFirst("sub").Value
// TODO use name to set the shadow property value like in the following post: https://www.meziantou.net/2017/07/03/entity-framework-core-generate-tracking-columns
}
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}
例如:
https://myaccount.blob.core.windows.net/mycontainer/myblob
如果您在容器中 get the blob item ,则可以将blob下载到NSString对象。
有关详细说明和代码,请参阅此article。
答案 1 :(得分:0)
let containerURL = "https://yourstoragename.blob.core.windows.net/\(ContainerName)\(SASToken)"
var container : AZSCloudBlobContainer
var error: NSError?
container = AZSCloudBlobContainer(url: NSURL(string: containerURL)! as URL, error: &error)
if ((error) != nil) {
print("Error in creating blob container object. Error code = %ld, error domain = %@, error userInfo = %@", error!.code, error!.domain, error!.userInfo);
}
else {
let blob = container.blockBlobReference(fromName: "Image.png")
blob.downloadToFile(withPath: fromfile, append: true, completionHandler:{(NSError) -> Void in
var blobUrl = blob.storageUri.primaryUri
})
}