避免在服务器上覆盖 AZURE blob
Avoid over-writing blobs AZURE on the server
我有一个 .NET 应用程序,它使用 WebClient 和 SAS 令牌将 blob 上传到容器。默认行为是同名的 blob 是 replaced/overwritten。
有没有办法在服务器上更改它,即防止替换已经存在的 blob?
我看过 Avoid over-writing blobs AZURE 但它是关于客户端的。
我的目标是防止服务器覆盖 blob。
据我所知,文件直接上传到容器而没有机会拦截请求并检查例如blob 的存在。
已编辑
让我澄清一下:我的客户端应用程序收到一个 SAS 令牌来上传一个新的 blob。但是,邪恶的黑客可以拦截令牌并上传具有现有名称的 blob。由于默认行为,新的 blob 将替换现有的(有效地删除好的)。
我知道处理客户端替换的不同方法。但是,我需要在服务器上执行此操作,甚至以某种方式针对客户端(这可能会被黑客破坏)。
服务器端没有配置,但您可以使用存储客户端 sdk 实现一些代码。
// 检索对先前创建的容器的引用。
var container = blobClient.GetContainerReference(containerName);
// 检索对 blob 的引用。
var blobreference = container.GetBlockBlobReference(blobName);
// 如果引用存在什么也不做
// 否则上传 blob。
您可以使用 REST 做类似的事情 api
https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/blob-service-rest-api
GetBlobProperties 如果 blob 不存在,它将 return 404。
Is there a way to change it on the server, i.e. prevents from replacing the already existing blob?
Azure 存储服务公开 Blob 服务 REST API 供您对 Blob 执行操作。对于 upload/update 一个 Blob(文件),您需要调用 Put Blob REST API,其状态如下:
The Put Blob operation creates a new block, page, or append blob, or updates the content of an existing block blob. Updating an existing block blob overwrites any existing metadata on the blob. Partial updates are not supported with Put Blob; the content of the existing blob is overwritten with the content of the new blob.
为了避免覆盖现有的 Blob,您需要明确指定 Conditional Headers for your Blob Operations. For a simple way, you could leverage Azure Storage SDK for .NET(本质上是对 Azure 存储 REST API 的包装)以将您的 Blob(文件)上传为遵循以避免覆盖 Blob:
try
{
var container = new CloudBlobContainer(new Uri($"https://{storageName}.blob.core.windows.net/{containerName}{containerSasToken}"));
var blob = container.GetBlockBlobReference("{blobName}");
//bool isExist=blob.Exists();
blob.UploadFromFile("{filepath}", accessCondition: AccessCondition.GenerateIfNotExistsCondition());
}
catch (StorageException se)
{
var requestResult = se.RequestInformation;
if(requestResult!=null)
//409,The specified blob already exists.
Console.WriteLine($"HttpStatusCode:{requestResult.HttpStatusCode},HttpStatusMessage:{requestResult.HttpStatusMessage}");
}
此外,在上传到 Azure Blob 存储之前,您可以将 blob 名称与 blob 文件的 MD5 代码结合起来。
据我所知,Azure Portal 或 Storage Tools 上没有任何配置可以让您在服务器端实现此目的。您可以尝试 post 您的 feedback 到 Azure 存储团队。
您可以颁发具有 "create" 权限和没有 "write" 权限的 SAS 令牌。这将允许用户上传最大 64 MB 的 blob(允许的最大 Put Blob),只要他们正在创建一个新的 blob 而不是覆盖现有的 blob。更多信息请参见SAS permissions的解释。
我有一个 .NET 应用程序,它使用 WebClient 和 SAS 令牌将 blob 上传到容器。默认行为是同名的 blob 是 replaced/overwritten。
有没有办法在服务器上更改它,即防止替换已经存在的 blob?
我看过 Avoid over-writing blobs AZURE 但它是关于客户端的。
我的目标是防止服务器覆盖 blob。
据我所知,文件直接上传到容器而没有机会拦截请求并检查例如blob 的存在。
已编辑
让我澄清一下:我的客户端应用程序收到一个 SAS 令牌来上传一个新的 blob。但是,邪恶的黑客可以拦截令牌并上传具有现有名称的 blob。由于默认行为,新的 blob 将替换现有的(有效地删除好的)。
我知道处理客户端替换的不同方法。但是,我需要在服务器上执行此操作,甚至以某种方式针对客户端(这可能会被黑客破坏)。
服务器端没有配置,但您可以使用存储客户端 sdk 实现一些代码。
// 检索对先前创建的容器的引用。
var container = blobClient.GetContainerReference(containerName);
// 检索对 blob 的引用。
var blobreference = container.GetBlockBlobReference(blobName);
// 如果引用存在什么也不做
// 否则上传 blob。
您可以使用 REST 做类似的事情 api https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/blob-service-rest-api
GetBlobProperties 如果 blob 不存在,它将 return 404。
Is there a way to change it on the server, i.e. prevents from replacing the already existing blob?
Azure 存储服务公开 Blob 服务 REST API 供您对 Blob 执行操作。对于 upload/update 一个 Blob(文件),您需要调用 Put Blob REST API,其状态如下:
The Put Blob operation creates a new block, page, or append blob, or updates the content of an existing block blob. Updating an existing block blob overwrites any existing metadata on the blob. Partial updates are not supported with Put Blob; the content of the existing blob is overwritten with the content of the new blob.
为了避免覆盖现有的 Blob,您需要明确指定 Conditional Headers for your Blob Operations. For a simple way, you could leverage Azure Storage SDK for .NET(本质上是对 Azure 存储 REST API 的包装)以将您的 Blob(文件)上传为遵循以避免覆盖 Blob:
try
{
var container = new CloudBlobContainer(new Uri($"https://{storageName}.blob.core.windows.net/{containerName}{containerSasToken}"));
var blob = container.GetBlockBlobReference("{blobName}");
//bool isExist=blob.Exists();
blob.UploadFromFile("{filepath}", accessCondition: AccessCondition.GenerateIfNotExistsCondition());
}
catch (StorageException se)
{
var requestResult = se.RequestInformation;
if(requestResult!=null)
//409,The specified blob already exists.
Console.WriteLine($"HttpStatusCode:{requestResult.HttpStatusCode},HttpStatusMessage:{requestResult.HttpStatusMessage}");
}
此外,在上传到 Azure Blob 存储之前,您可以将 blob 名称与 blob 文件的 MD5 代码结合起来。
据我所知,Azure Portal 或 Storage Tools 上没有任何配置可以让您在服务器端实现此目的。您可以尝试 post 您的 feedback 到 Azure 存储团队。
您可以颁发具有 "create" 权限和没有 "write" 权限的 SAS 令牌。这将允许用户上传最大 64 MB 的 blob(允许的最大 Put Blob),只要他们正在创建一个新的 blob 而不是覆盖现有的 blob。更多信息请参见SAS permissions的解释。