簡體   English   中英

如何將文件上傳到 blob 存儲並從附件中的 cosmosdb 文檔中引用它

[英]How upload file to blob storage and reference it from cosmosdb document in attachment

我是 CosmosDb 和 azure blob 存儲的新手,作為一項要求,我需要引用從 CosmosDb 中的文檔上傳到 azure blob 存儲的文件,並在附件部分使用它來保存元數據。

我知道 json 元數據結構應該是這樣的:

{    
   "id":"image13d65101-90c4-4c2a-a423-fbf221c73233",  
   "contentType":"image/jpg",  
   "media":"www.bing.com",  
   "_rid":"rnYYAMVFUAUBAAAAAAAAAEC+LNM=",  
   "_ts":1408056025,  
   "_self":"dbs\/rnYYAA==\/colls\/rnYYAMVFUAU=\/docs\/rnYYAMVFUAUBAAAAAAAAAA==\/attachments\/rnYYAMVFUAUBAAAAAAAAAEC+LNM=",  
   "_etag":"00002a00-0000-0000-0000-53ed3ad90000"  
}

但是如何在將文件上傳到 azure blob 存儲時獲取對媒體屬性的引用,更准確地說,如何將文件從 c# 上傳到 azure blob 存儲並通過將 url 設置為媒體屬性來引用它。

這是我的一個應用程序中的示例代碼,用於上傳到 blob 存儲,然后將 cosmosdb 的引用保存為 uri,

    public async Task<IActionResult> UploadImageAsync([FromBody] ImageUploadRequest imageRequest)
    {

        if (string.IsNullOrEmpty(imageRequest?.Base64))
        {
            return BadRequest();
        }

        var tokens = imageRequest.Base64.Split(',');
        var ctype = tokens[0].Replace("data:", "");
        var base64 = tokens[1];
        var content = Convert.FromBase64String(base64);

        // Upload photo to storage...
        var blobUri = await UploadImageToStorage(content);

        // Then create a Document in CosmosDb to notify our Function
        var identifier = await UploadDocument(blobUri, imageRequest.Name ?? "Bob");

        return Ok(identifier);
    }

    private async Task<Guid> UploadDocument(Uri uri, string imageName)
    {

        var endpoint = new Uri(_settings.ImageConfig.CosmosUri);
        var auth = _settings.ImageConfig.CosmosKey;
        var client = new DocumentClient(endpoint, auth);
        var identifier = Guid.NewGuid();

        await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = dbName });
        await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(dbName),
            new DocumentCollection { Id = colName });

        await client.CreateDocumentAsync(
            UriFactory.CreateDocumentCollectionUri(dbName, colName),
            new ImageDocument
            {
                Id = identifier,
                IsApproved = null,
                PetName = petName,
                MediaUrl = uri.ToString(),
                Created = DateTime.UtcNow
            });

        return identifier;
    }

    private async Task<Uri> UploadImageToStorage(byte[] content)
    {
        var storageName = _settings.PetsConfig.BlobName;
        var auth = _settings.PetsConfig.BlobKey;
        var uploader = new PhotoUploader(storageName, auth);
        var blob = await uploader.UploadPetPhoto(content);
        return blob.Uri;
    }

如何將文件從 c# 上傳到 azure blob 存儲並通過將 url 設置為 media 屬性來引用它。

可以參考這篇文章設置和檢索元數據,上傳后應該會再次獲取blob。

示例代碼如下:

        //the code to get the blob again after uploading.
        var blockblob = blobContainer.GetBlockBlobReference(blobname);

        //the code to set medadata.
        blockblob.FetchAttributes();
        blockblob.Metadata["media"] = "www.bing.com";
        blockblob.SetMetadata();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM