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

我怕爱的太早我们不能终老 提交于 2020-06-13 09:41:41

问题


I'm new to CosmosDb and azure blob storage, as a requirement I need to reference a file uploaded to azure blob storage from a document in CosmosDb and use it in the attachment section to save the metadata.

I know the json metadata stucture should be like this:

{    
   "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"  
}

but how can I get a reference to the media property when uploading the file to azure blob storage, more accurately how do I upload a file from c# to azure blob storage and reference it by setting the url to the media property.


回答1:


Here is a sample code from one of my application to upload to blob storage and then save the reference to the cosmosdb as a 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;
    }



回答2:


how do I upload a file from c# to azure blob storage and reference it by setting the url to the media property.

You can refer to this article Setting and retrieving metadata, you should get the blob again after uploading it.

Sample code like this:

        //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();


来源:https://stackoverflow.com/questions/55996696/how-upload-file-to-blob-storage-and-reference-it-from-cosmosdb-document-in-attac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!