在重命名前一个图像后,我试图使用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);
预先感谢
答案 0 :(得分:0)
1-重命名旧的图像名称:例如'xama_452_11_2018“
由于缺少用于在Azure上重命名blob文件的API,您可以使用所需的格式设置newBlobName并将源复制到目标。请参阅此article。
2-将其移动到“ oldcontainer”容器中
您可以获取目标容器的blob以复制源。请参阅此one。
3-然后将新图像保存在“ imagecontainer”中
将blob上载到sourcecontainer。请参阅此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);
}
如果您仍有任何问题,请随时告诉我。希望对您有帮助。