Azure 存储 - 此代码有任何计时问题吗?
Azure storage - Any timing issues with this code?
我正在使用 Azure 存储来存储和检索图像。我有这种方法来保存图像(blob):
public void SaveBlob(string containerName, string blobName, byte[] blob)
{
// Retrieve reference to the blob
CloudBlockBlob blockBlob = GetContainer(containerName, true).GetBlockBlobReference(blobName);
using (var stream = new MemoryStream(blob, writable: false))
{
blockBlob.UploadFromStream(stream);
}
}
这是 GetContainer 方法:
public CloudBlobContainer GetContainer(string containerName, bool createIfNotExist)
{
if (string.IsNullOrEmpty(containerName))
return null;
// Retrieve a reference to a container. If container doesn't exist, optionally create it.
CloudBlobContainer container = this._blobClient.GetContainerReference(containerName);
if (container == null && !createIfNotExist)
return null;
// Create the container if it doesn't already exist. It will be private by default.
container.CreateIfNotExists(BlobContainerPublicAccessType.Off, null, null);
return container;
}
这里发生的事情是,当我尝试保存 blob 时,我首先获得了对容器的引用。如果容器不存在,则会创建它,然后保存 blob。如果我必须先创建容器然后立即将 blob 保存到其中,我会 运行 陷入计时问题吗?我担心的是,我可能会在 Azure 完成创建之前尝试将 blob 保存到容器中。或许这不是问题?
查看您的代码,我认为您不会 运行 遇到任何时间问题,因为 CreateIfNotExists
方法是一种同步方法,并且只会在创建容器时 return .同样对于 Azure Blob 存储(与 Amazon S3 不同),该方法将立即创建容器,或者在创建失败时抛出错误(或者换句话说,Azure 存储是 Strongly Consistent
)。
另外我觉得这段代码是多余的:
if (container == null && !createIfNotExist)
return null;
因为 container
永远不会等于 null
因此这个 if
条件永远不会是 true
.
我正在使用 Azure 存储来存储和检索图像。我有这种方法来保存图像(blob):
public void SaveBlob(string containerName, string blobName, byte[] blob)
{
// Retrieve reference to the blob
CloudBlockBlob blockBlob = GetContainer(containerName, true).GetBlockBlobReference(blobName);
using (var stream = new MemoryStream(blob, writable: false))
{
blockBlob.UploadFromStream(stream);
}
}
这是 GetContainer 方法:
public CloudBlobContainer GetContainer(string containerName, bool createIfNotExist)
{
if (string.IsNullOrEmpty(containerName))
return null;
// Retrieve a reference to a container. If container doesn't exist, optionally create it.
CloudBlobContainer container = this._blobClient.GetContainerReference(containerName);
if (container == null && !createIfNotExist)
return null;
// Create the container if it doesn't already exist. It will be private by default.
container.CreateIfNotExists(BlobContainerPublicAccessType.Off, null, null);
return container;
}
这里发生的事情是,当我尝试保存 blob 时,我首先获得了对容器的引用。如果容器不存在,则会创建它,然后保存 blob。如果我必须先创建容器然后立即将 blob 保存到其中,我会 运行 陷入计时问题吗?我担心的是,我可能会在 Azure 完成创建之前尝试将 blob 保存到容器中。或许这不是问题?
查看您的代码,我认为您不会 运行 遇到任何时间问题,因为 CreateIfNotExists
方法是一种同步方法,并且只会在创建容器时 return .同样对于 Azure Blob 存储(与 Amazon S3 不同),该方法将立即创建容器,或者在创建失败时抛出错误(或者换句话说,Azure 存储是 Strongly Consistent
)。
另外我觉得这段代码是多余的:
if (container == null && !createIfNotExist)
return null;
因为 container
永远不会等于 null
因此这个 if
条件永远不会是 true
.