【问题标题】:Rename blob then save new重命名 blob 然后保存新的
【发布时间】:2019-04-14 08:01:06
【问题描述】:

在重命名之前的图像后,我正在尝试使用 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);

提前致谢

【问题讨论】:

标签: c# azure xamarin.forms azure-active-directory azure-storage


【解决方案1】:

1- 重命名旧图像名称:例如“xama_452_11_2018”

由于缺少在 Azure 上重命名 blob 文件的 API,您可以将 newBlobName 设置为您想要的格式并将源复制到目标。参考这个article

2-将其移动到容器“oldcontainer”中

您可以获取 dectionation 容器的 blob 来复制源。参考这个one

3-然后将新图像保存在“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);
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多