问题
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).
回答1:
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.
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
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);
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.
回答2:
Our PHP tutorial for blobs shows how to create a connection string and then upload a blob.
You can use the SAS as the credentials for your connection string. The connection string format is (line breaks included for readability only):
BlobEndpoint=myBlobEndpoint;
QueueEndpoint=myQueueEndpoint;
TableEndpoint=myTableEndpoint;
FileEndpoint=myFileEndpoint;
SharedAccessSignature=sasToken
Please see Create a connection string using a shared access signature for more information and examples.
来源:https://stackoverflow.com/questions/39241302/how-can-i-upload-the-blob-in-azure-using-the-shared-access-signature-using-php