【问题标题】:how to get blob-URL after file upload in azure如何在 azure 中上传文件后获取 blob-URL
【发布时间】:2013-06-02 10:51:06
【问题描述】:

我正在尝试连接网络和辅助角色。所以我有一个页面,用户可以上传视频文件。文件很大,所以我不能使用查询来发送文件。这就是为什么我试图将它们上传到 Blob 存储然后通过查询发送 url。但我不知道如何获取此网址。

谁能帮帮我?

【问题讨论】:

  • 什么意思:文件很大,所以我不能使用查询发送文件?
  • 我说的是视频文件

标签: azure azure-blob-storage azure-web-roles


【解决方案1】:

假设您使用 .Net 存储客户端库通过创建 CloudBlockBlob 的实例将 blob 上传到 blob 存储,您可以通过读取 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;
}

View example on Pastebin

【讨论】:

  • 您能否输入一些代码来获取最近上传的 blob(图像文件)的 URL?
  • TL;DR:只需使用cloudBlockBlob.Uri.AbsoluteUri
  • 新图书馆怎么样? (截至目前第 12 版)
  • @devmiles.com...获取 CloudBlob 的实例。它有一个 Uri 属性 (docs.microsoft.com/en-us/dotnet/api/…)
【解决方案2】:

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)

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

【讨论】:

  • 是的!它没有记录,我通过 .Net 文档了解它!
【解决方案3】:

截至 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));

【讨论】:

  • 当我尝试访问浏览器上的图像时,我收到一个错误此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。
  • 感谢您的回答。 2022 年仍在工作。
【解决方案4】:

当您使用更新后的“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;

【讨论】:

    【解决方案5】:

    这里是 V12 方式。我不能保证我的变量名是准确的,但是代码可以工作。

    protected BlobContainerClient AzureBlobContainer
    {
      get
      {
        if (!isConfigurationLoaded) { throw new Exception("AzureCloud currently has no configuration loaded"); }
        if (_azureBlobContainer == null)
        {
          if (!string.IsNullOrEmpty(_configuration.StorageEndpointConnection))
          {
    
            BlobServiceClient blobClient = new BlobServiceClient(_configuration.StorageEndpointConnection);
            BlobContainerClient container = blobClient.GetBlobContainerClient(_configuration.StorageContainer);
            container.CreateIfNotExists();
            _azureBlobContainer = container;
          }
        }
        return _azureBlobContainer;
      }
    }
    
    public bool UploadFileToCloudStorage(string fileName, Stream fileStream)
    {
      BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
      cloudFile.DeleteIfExists();
      fileStream.Position = 0;
      cloudFile.Upload(fileStream);
      return true;
    }
    
    public BlobClient UploadFileToCloudStorageWithResults(string fileName, Stream fileStream)
    {
      BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
      cloudFile.DeleteIfExists();
      fileStream.Position = 0;
      cloudFile.Upload(fileStream);
    
      return cloudFile;
    }
    
    public Stream DownloadFileStreamFromCloudStorage(string fileName)
    {
      BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
      Stream fileStream = new MemoryStream();
      cloudFile.DownloadTo(fileStream);
      return fileStream;
    }
    

    【讨论】:

      【解决方案6】:

      嘿,对不起,我不知道我是如何在答案中再次发布相同的评论的。请在下面找到我的正确答案,并详细说明此存储 blob 如何从 blob 获取 url。

      // 在 web.config 文件中添加连接字符串,以便您在需要时轻松访问多个位置。

      <connectionStrings>
      
      <add name="BlobStorageConnection" connectionString="DefaultEndpointsProtocol=https;AccountName=accName;AccountKey=xxxxxxxxxxxxxxxxxx YOU WILL FIND THIS in your AZURE ACCOUNT xxxxxxxxxx==;EndpointSuffix=core.windows.net"/>
      

      这是您可以从 web.config 文件中获取的字符串。

      string BlobConnectionString = ConfigurationManager.ConnectionStrings["BlobStorageConnection"].ConnectionString;
           public string GetFileURL()
              {
              //This will create the storage account to get the details of account.
              CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(BlobConnectionString);
      
              //create client
              CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
      
              //Get a container
              CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("ContainerName");
      
              //From here we will get the URL of file available in Blob Storage.
              var blob1 = cloudBlobContainer.GetBlockBlobReference(imageName);
              string FileURL=blob1.Uri.AbsoluteUri;
              return FileURL;
      
          }
      

      如果您有文件(或图像)名称,则可以通过这种方式获取文件的 url。

      【讨论】:

      • 你们俩是不是在其他地方找到了相同的答案,还是只是抄袭了 Gaurav 的答案?
      • @nivs1978 我不知道它是如何发布的。从存储blob账户获取文件url的正确详细解释请参考。
      猜你喜欢
      • 2020-04-12
      • 2019-12-22
      • 1970-01-01
      • 2020-04-12
      • 2016-02-13
      • 1970-01-01
      • 2019-12-31
      • 2021-03-27
      • 2020-09-24
      相关资源
      最近更新 更多