我对一些图书馆的停办感到困惑和忧伤。
我想将文本文件上传到Azure Blob Storage。但是这个功能不起作用。
private static function output($path, $text): bool
{
$accountName = config('filesystems.disks.azure.account_name');
$containerName = config('filesystems.disks.azure.container_name');
$sasToken = config('filesystems.disks.azure.sas_token');
$uri = "https://{$accountName}.blob.core.windows.net/{$containerName}/{$path}";
$version = '2024-05-04';
$checkUri = "{$uri}?{$sasToken}";
$checkResponse = Http::withHeaders([
'x-ms-version' => $version,
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
])->head($checkUri);
if ($checkResponse->status() == 404) {
$createUri = "{$uri}?{$sasToken}";
$createResponse = Http::withHeaders([
'x-ms-blob-type' => 'AppendBlob',
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
'x-ms-version' => $version,
])->put($createUri);
if (!$createResponse->successful()) {
throw new \Exception('Failed to create new blob: ' . $createResponse->body());
}
}
$appendUri = "{$uri}?comp=appendblock&{$sasToken}";
$response = Http::withHeaders([
'Content-Type' => 'application/octet-stream',
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
'x-ms-version' => $version,
])->put($appendUri, $text);
if ($response->successful()) {
return true;
} else {
throw new \Exception('Failed to append content: ' . $response->body());
}
}
错误信息 我不知道这个错误的原因。
Failed to create new blob: <?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidHeaderValue</Code><Message>The value for one of the HTTP headers is not inn the correct format.
Time:2024-05-08T02:04:08.7165841Z</Message><HeaderName>Content-Length</HeaderName><HeaderValue>2</HeaderValue></Error>
encoding="utf-8"?><Error><Code>InvalidHeaderValue</Code><Message>The value for one of the HTTP headers is not in the correct format. Time:2024-05-08T02:04:08.7165841Z</Message><HeaderName>Content-Length</HeaderName><HeaderValue>2</HeaderValue></Error>
出现上述错误是因为您没有在 API 请求中使用
'Content-Length'
标头。
注意: 根据此 MS-Document 仅当使用
设置为Append Block
创建 Blob 时才允许x-ms-blob-type
操作。AppendBlob
在我的环境中,我有一个名为
testblob.txt
的附加 blob,如下所示:
传送门:
现在,使用下面的代码,它将 blob 添加到我的文本文件中。
代码:
private static function output($path, $text): bool
{
$accountName = config('filesystems.disks.azure.account_name');
$containerName = config('filesystems.disks.azure.container_name');
$sasToken = config('filesystems.disks.azure.sas_token');
$uri = "https://{$accountName}.blob.core.windows.net/{$containerName}/{$path}";
$version = '2024-05-04';
$checkUri = "{$uri}?{$sasToken}";
$checkResponse = Http::withHeaders([
'x-ms-version' => $version,
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
])->head($checkUri);
if ($checkResponse->status() == 404) {
$createUri = "{$uri}?{$sasToken}";
$createResponse = Http::withHeaders([
'x-ms-blob-type' => 'AppendBlob',
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
'x-ms-version' => $version,
])->put($createUri);
if (!$createResponse->successful()) {
throw new \Exception('Failed to create new blob: ' . $createResponse->body());
}
}
$appendUri = "{$uri}?comp=appendblock&{$sasToken}";
$length = strlen($text);
$response = Http::withHeaders([
'Content-Type' => 'application/octet-stream',
'Content-Length' => $length,
'x-ms-date' => gmdate('D, d M Y H:i:s T'),
'x-ms-version' => $version,
])->put($appendUri, $text);
if ($response->successful()) {
return true;
} else {
throw new \Exception('Failed to append content: ' . $response->body());
}
}
我提供了
$text='The cat sat on the mat. The quick brown fox jumps over the lazy dog.'
。附加 blob。
输出:
以上执行并追加成功。
通过从 blob 存储下载文件进行验证。
文件: