我如何使用PHP使用共享访问签名以Azure方式上传Blob? [英] how can i upload the blob in azure using the shared access signature using PHP?

查看:174
本文介绍了我如何使用PHP使用共享访问签名以Azure方式上传Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用azure php SDK,并且拥有从应用程序中获取的SAS签名,现在该如何使用SAS(共享访问签名)将文件上传到Blob.

I am using the azure php SDK and I have the SAS signature which I get from the application now how can I upload the file to the blob using the SAS( Shared Access Signature).

推荐答案

如果您拥有SAS令牌,并且具有Azure存储帐户中的blob写入权限,则可以利用SAS令牌直接上传文件到没有适用于PHP的Azure存储SDK的Azure存储.

If you have an SAS token, and if it has the write permission to blob in your Azure Storage Account, you can leverage the SAS token to upload your files directly to Azure Storage without Azure Storage SDK for PHP.

我假定您具有带签名的正确SAS查询字符串,该字符串应类似于: ?sv=2015-04-05&ss=bt&srt=sco&sp=w&st=2016-09-01T01%3A54%3A00Z&se=2016-09-02T01%3A54%3A00Z&sig=AQ%2F1yL8bt0AQzoYwtQmTUR6UKkJPC4PXg%2BxysdlkMoE%3D

I assume that you have the correct SAS query string with signature, which should be similar with: ?sv=2015-04-05&ss=bt&srt=sco&sp=w&st=2016-09-01T01%3A54%3A00Z&se=2016-09-02T01%3A54%3A00Z&sig=AQ%2F1yL8bt0AQzoYwtQmTUR6UKkJPC4PXg%2BxysdlkMoE%3D

然后,您利用cUrl向Azure存储创建REST API请求以上传文件,

Then you leverage cUrl to create REST API request to Azure Storage to upload your file,

$sas= '?sv=2015-04-05&ss=bt&srt=sco&sp=rwl&st=2016-09-01T01%3A54%3A00Z&se=2016-09-02T01%3A54%3A00Z&sig=To%2BITizcZWTgr4I6o9BI%2Bdv34CBkuhV5%2BSEQXnaJr%2B0%3D';

$endpoint = 'https://<storage_account>.blob.core.windows.net';
$container = 'test';
$blob = 'test.png';
$url = $endpoint.'/'.$container.'/'.$blob.$sas;

$uploadfile ="test.PNG";
$content = file_get_contents($uploadfile);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-ms-blob-type: BlockBlob','Content-Length: ' . strlen($content)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,$content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);

您可以参考 https://msdn.microsoft.com/zh-cn/library/azure/dd179451.aspx 有关将blob放入Azure存储的REST API的更多详细信息.

You can refer to https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx for more details about the REST APIs for putting blobs to Azure Storage.

这篇关于我如何使用PHP使用共享访问签名以Azure方式上传Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆