在Azure中添加,编辑元数据 [英] Add , Edit Metadata in Azure
问题描述
我最近在这里提出了一个问题,多亏了Gaurav Mantri,我可以将元数据添加到blob azure中. 在AzureBlobStorage类中编辑后的代码:
I recently asked a question here and thanks to Gaurav Mantri I could add Metadata to blob azure . my Code after editing in AzureBlobStorage class :
public void SaveMetaData(string fileName, string container, string key, string value)
{
var blob = GetBlobReference(fileName, container);
blob.FetchAttributes();
blob.Metadata.Add(key, value);
blob.SetMetadata();
}
然后我从myController调用它:
and I call it from myController by this :
public JsonResult SaveMetaData(string name, string key, int id)
{
var uploadedFils = _FileStorage.GetUploadedFiles("images", id + "/");
if (!uploadedFils.Any())
_FileStorage.SaveMetaData(name, "images", key, "true");
foreach (var file in uploadedFils)
{
if (name == file.Name)
{
_FileStorage.SaveMetaData(FormatFileName(id, name), "images", key, "true");
}
else
{
_FileStorage.SaveMetaData(FormatFileName(id, file.Name), "images", key, "false");
}
}
return Json("");
}
获取上传文件的代码
public IEnumerable<Attachment> GetUploadedFiles(string container, string blobprefix)
{
if (string.IsNullOrWhiteSpace(container))
container = DefaultBlobContainer;
var storageAccount = CreateStorageAccountFromConnectionString(GetStorageConnectionString());
var blobContainer = GetBlobContainer(storageAccount, container);
var resultList = new List<Attachment>();
try
{
foreach (IListBlobItem item in blobContainer.ListBlobs(blobprefix, false))
{
var blob = (CloudBlockBlob) item;
var file = new Attachment
{
Name = blob.Name.Substring(blob.Name.LastIndexOf('/') + 1),
Size = blob.Properties.Length,
Extension = Path.GetExtension(blob.Name)
};
resultList.Add(file);
}
}
catch (Exception e)
{
}
return resultList;
}
,当我单击想要设置为active的所需图像时,我将调用此操作. 第一次可以使用,但是我不知道如何编辑它以进行第二次单击,特别是这是我第一次使用Azure? 这行背后的逻辑是:当图库为空并且用户上传第一张图像时,该图像将自动设置为活动状态:
and I call this action when I click on the desired image that I want to set as active . for first time it works , but I don't know how to edit it for second click ,specially this is first time for me dealing with Azure ? the logic behind this line that : when the Gallery is empty and the users upload the first image , this image will be set automatically to active:
if (!uploadedFils.Any())
_FileStorage.SaveMetaData(name, "images", key, "true");
推荐答案
根据您的描述,我检查了您的代码,您需要按以下方式修改代码:
According to your description, I checked your code, you need to modify your code as follows:
SaveMetaData 方法:
public void SaveMetaData(string fileName, string container, string key, string value)
{
var blob = GetBlobReference(fileName, container);
blob.FetchAttributes();
if (blob.Metadata.ContainsKey(key))
{
blob.Metadata[key] = value;
}
else
blob.Metadata.Add(key, value);
blob.SetMetadata();
}
根据您的方案,您的图像文件将被上载到images\{id}\{filename}
.并且在调用控制器下的 SaveMetaData 之前,需要确保具有特定参数name
和id
的文件存在于Blob存储中.我假设您需要删除以下代码段:
Based on your scenario, your image files would be uploaded to images\{id}\{filename}
. And before you invoke the SaveMetaData under your controller, you need to make sure the file with the specific parameters name
and id
exists in your blob storage. I assumed that you need to remove the following code snippet:
if (!uploadedFils.Any())
FileStorage.SaveMetaData(name, "images", key, "true");
注意:如果没有文件,则无法为其添加/更新元数据.同样,您只需为参数fileName
设置name
而不组合id
.根据我的理解,SaveMetaData
方法用于设置现有文件的元数据.我建议您将上述逻辑转移到用于上传文件的操作上,并在没有文件的情况下设置默认的元数据.
Note: If there has no files, you could not add/update the meta data for it. Also, you just set the name
for the parameter fileName
without combining the id
. Based on my understanding, the SaveMetaData
method is used to set meta data for existing files. I recommend you move the above logic to the action for uploading the file and set the default meta data if there has no files.
这篇关于在Azure中添加,编辑元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!