You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中获取Blob标签?

在Laravel中获取Azure Blob标签的实现思路

思路一:利用社区维护的Flysystem Azure适配器

Laravel默认基于Flysystem实现文件系统,社区维护的league/flysystem-azure-blob-storage适配器可以对接Azure Blob Storage,支持调用底层SDK方法获取标签:

  • 安装依赖包:
composer require league/flysystem-azure-blob-storage
  • 在config/filesystem.php中配置Azure磁盘:
'disks' => [
    // 其他磁盘配置
    'azure' => [
        'driver'    => 'azure',
        'account'   => env('AZURE_STORAGE_ACCOUNT'),
        'key'       => env('AZURE_STORAGE_KEY'),
        'container' => env('AZURE_STORAGE_CONTAINER'),
        'url'       => env('AZURE_STORAGE_URL'),
        'endpoint'  => env('AZURE_STORAGE_ENDPOINT'),
    ],
],
  • 获取Blob标签的代码示例:
use Illuminate\Support\Facades\Storage;
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;

// 获取配置好的Azure磁盘实例
$disk = Storage::disk('azure');

// 取出底层适配器和Azure Blob客户端
/** @var AzureBlobStorageAdapter $adapter */
$adapter = $disk->getAdapter();
$blobClient = $adapter->getClient();

// 指定目标容器和Blob路径
$containerName = 'your-container-name';
$blobName = 'path/to/target/blob';

// 获取Blob标签(返回键值对数组)
$tags = $blobClient->getBlobTags($containerName, $blobName);

// 打印查看标签
dd($tags);

思路二:直接调用Azure Blob Storage REST API

如果不想依赖第三方包,可以直接通过HTTP请求调用Azure的REST接口获取标签,核心是处理认证签名:

  • 用Laravel Http客户端实现的示例:
use Illuminate\Support\Facades\Http;

$accountName = env('AZURE_STORAGE_ACCOUNT');
$accountKey = env('AZURE_STORAGE_KEY');
$containerName = 'your-container-name';
$blobName = 'path/to/target/blob';

// 构造请求URL
$url = "https://{$accountName}.blob.core.windows.net/{$containerName}/{$blobName}?comp=tags";

// 生成符合要求的UTC时间戳
$date = gmdate('D, d M Y H:i:s \G\M\T');

// 构造签名所需的字符串
$stringToSign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:{$date}\nx-ms-version:2021-06-08\n/{$accountName}/{$containerName}/{$blobName}\ncomp:tags";

// 生成SharedKey签名
$signature = base64_encode(hash_hmac('sha256', $stringToSign, base64_decode($accountKey), true));

// 发送请求并处理响应
$response = Http::withHeaders([
    'x-ms-date' => $date,
    'x-ms-version' => '2021-06-08', // 需使用支持Blob标签的API版本
    'Authorization' => "SharedKey {$accountName}:{$signature}",
])->get($url);

if ($response->successful()) {
    $tags = $response->json();
    dd($tags);
} else {
    // 处理请求错误
    dd($response->body());
}

内容的提问来源于stack exchange,提问作者HARAKAT Abdellah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:25:03