gpt4 book ai didi

c# - 重命名 blob 然后保存新的

转载 作者:行者123 更新时间:2023-12-03 02:55:16 27 4
gpt4 key购买 nike

我正在尝试使用 Xamarin Blob 在重命名前一个图像后保存图像的新版本。例如,我的旧图像位于名为“imagecontainer”的容器中,并且 ID 名为“xama_452”

what I would like to do is :

1- Rename the old image name : for example 'xama_452_11_2018"
2-Move it in a container "oldcontainer"
3- then save the new image in "imagecontainer"

我尝试了一些代码,我可以上传图像/blob,但无法重命名它并将其移动到另一个容器。

 protected static async Task<CloudBlockBlob> SaveBlockBlob(string containerName, byte[] blob, string blobTitle)
{

var blobContainer = GetBlobContainer(containerName);
var blockBlob = blobContainer.GetBlockBlobReference(blobTitle);

var oldBlob = blobContainer.GetBlockBlobReference(blockBlob.Uri.ToString());
var newBlob = blobContainer.GetBlockBlobReference(blockBlob.Uri.ToString().Replace(blobTitle, DateTime.UtcNow.ToString()+ blobTitle));


await newBlob.StartCopyAsync(oldBlob);

// here is the methode to upload
// await blockBlob.UploadFromByteArrayAsync(blob, 0, blob.Length).ConfigureAwait(false);

return blockBlob;
}

// method to get blob's container
static CloudBlobContainer GetBlobContainer(string containerName) => BlobClient.GetContainerReference(containerName);

提前致谢

最佳答案

1- Rename the old image name : for example 'xama_452_11_2018"

由于 Azure 上缺乏用于重命名 Blob 文件的 API,您可以使用所需的格式设置 newBlobName 并将源复制到目标。引用这个article .

2-Move it in a container "oldcontainer"

您可以获取目标容器的 blob 来复制源。引用这个one .

3- then save the new image in "imagecontainer"

将 blob 上传到源容器。引用这个article .

全部代码如下:

public static void RenameBlob(string containerName, string destContainer,string blobName,string newblobname)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imgcontainer = cloudBlobClient.GetContainerReference(containerName);
string[] name = blobName.Split('.');
//rename blob
string newBlobName = name[0] + "_"+DateTime.Now.ToString("MM")+"_"+DateTime.Now.ToString("yyyy") + "." + name[1];
CloudBlobContainer oldcontainer = cloudBlobClient.GetContainerReference(destContainer);
if (!oldcontainer.Exists())
{
throw new Exception("Destination container does not exist.");
}
CloudBlockBlob blobCopy = oldcontainer.GetBlockBlobReference(newBlobName);
if (!blobCopy.Exists())
{
CloudBlockBlob blob = imgcontainer.GetBlockBlobReference(blobName);
if (blob.Exists())
{
//move blob to oldcontainer
blobCopy.StartCopy(blob);
blob.Delete();
}
}
//upload blob to imagecontainer
CloudBlockBlob cloudblobnew = imgcontainer.GetBlockBlobReference(newblobname);
cloudblobnew.UploadFromFileAsync(newfile);
}

如果您仍有任何问题,请随时告诉我。希望对您有帮助。

关于c# - 重命名 blob 然后保存新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com