如何在azure中上传文件后获取blob-URL

Sta*_*ger 34 azure azure-storage-blobs azure-web-roles

我正在尝试连接web和worker角色.所以我有一个用户可以上传视频文件的页面.文件很大,所以我不能使用查询来发送文件.这就是我试图将它们上传到Blob存储然后通过查询发送URL的原因.但我不知道如何得到这个网址.

谁能帮我?

Gau*_*tri 63

假设您通过创建实例使用.Net存储客户端库将blob上载到blob存储中CloudBlockBlob,您可以通过读取blob的Uri属性来获取blob的URL .

static void BlobUrl()
{
    var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    var cloudBlobClient = account.CreateCloudBlobClient();
    var container = cloudBlobClient.GetContainerReference("container-name");
    var blob = container.GetBlockBlobReference("image.png");
    blob.UploadFromFile("File Path ....");//Upload file....

    var blobUrl = blob.Uri.AbsoluteUri;
}
Run Code Online (Sandbox Code Playgroud)

查看Pastebin上的示例

  • TL; DR:只需使用``cloudBlockBlob.Uri.AbsoluteUri`` (3认同)
  • 新图书馆怎么样?(截至目前第 12 节) (2认同)

Duc*_*Mai 10

截至 2020 年的完整工作 Javascript 解决方案:

const { BlobServiceClient } = require('@azure/storage-blob')

const AZURE_STORAGE_CONNECTION_STRING = '<connection string>'

async function main() {
  // Create the BlobServiceClient object which will be used to create a container client
  const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

  // Make sure your container was created
  const containerName = 'my-container'

  // Get a reference to the container
  const containerClient = blobServiceClient.getContainerClient(containerName);
  // Create a unique name for the blob
  const blobName = 'quickstart.txt';

  // Get a block blob client
  const blockBlobClient = containerClient.getBlockBlobClient(blobName);

  console.log('\nUploading to Azure storage as blob:\n\t', blobName);

  // Upload data to the blob
  const data = 'Hello, World!';
  await blockBlobClient.upload(data, data.length);
  
  console.log("Blob was uploaded successfully. requestId: ");
  console.log("Blob URL: ", blockBlobClient.url)
}

main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答。2022年还在工作。 (2认同)

log*_*i98 10

当您使用更新的“Azure 存储 Blob”包时,请使用以下代码。


BlobClient blobClient = containerClient.GetBlobClient("lg.jpg");

Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", containerClient.Uri);

using FileStream uploadFileStream = File.OpenRead(fileName);

if (uploadFileStream != null) 
    await blobClient.UploadAsync(uploadFileStream, true);

var absoluteUrl= blobClient.Uri.AbsoluteUri;
Run Code Online (Sandbox Code Playgroud)


gpa*_*paw 5

对于python用户,您可以使用 blob_client.url

不幸的是,它没有记录在 docs.microsoft.com 中

from azure.storage.blob import BlobServiceClient
# get your connection_string - look at docs
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(storage_container_name)
You can call blob_client.url
blob_client = container_client.get_blob_client("myblockblob")
with open("pleasedelete.txt", "rb") as data:
    blob_client.upload_blob(data, blob_type="BlockBlob")
print(blob_client.url)
Run Code Online (Sandbox Code Playgroud)

将返回 https://pleasedeleteblob.blob.core.windows.net/pleasedelete-blobcontainer/myblockblob