Skip to content

Instantly share code, notes, and snippets.

@kaz29
Created November 1, 2019 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaz29/e8e73c8cca3e66c2a831e66c3ba5ba72 to your computer and use it in GitHub Desktop.
Save kaz29/e8e73c8cca3e66c2a831e66c3ba5ba72 to your computer and use it in GitHub Desktop.
Create SAS
{
"name": "kaz/sastest",
"authors": [
{
"name": "Kaz Watanabe",
"email": ""
}
],
"require": {
"microsoft/azure-storage-blob": "^1.4",
"guzzlehttp/guzzle": "^6.4",
"cakephp/chronos": "^1.2"
}
}
<?php
require './vendor/autoload.php';
use Cake\Chronos\Chronos;
use GuzzleHttp\Client;
use MicrosoftAzure\Storage\Blob\BlobSharedAccessSignatureHelper;
use MicrosoftAzure\Storage\Blob\Internal\BlobResources;
use MicrosoftAzure\Storage\Common\Internal\Resources;
$accountName = getenv('STORAGE_ACCOUNT_NAME');
$accountKey = getenv('STORAGE_ACCOUNT_KEY');
$containerName = getenv('CONTAINER_NAME');
$helper = new BlobSharedAccessSignatureHelper($accountName, $accountKey);
$sas = $helper->generateBlobServiceSharedAccessSignatureToken(
Resources::RESOURCE_TYPE_BLOB,
"{$containerName}/composer.json",
'w',
Chronos::now(new \DateTimeZone('UTC'))->addSeconds(60)->format('Y-m-d\TH:i:s\Z'),
Chronos::now(new \DateTimeZone('UTC'))->subSeconds(10)->format('Y-m-d\TH:i:s\Z'),
'', // リクエスト元のIPアドレス
'https'
);
echo "sas: {$sas}\n";
try {
$client = new Client(['base_uri' => sprintf('https://%s.%s/%s/', $accountName, Resources::BLOB_BASE_DNS_NAME, $containerName)]);
$response = $client->put('composer.json', [
'body' => file_get_contents('./composer.json'),
'headers' => [
'x-ms-version' => BlobResources::STORAGE_API_LATEST_VERSION,
'x-ms-blob-type' => 'BlockBlob',
],
'query' => $sas,
]);
echo sprintf("StatusCode: %d\n", $response->getStatusCode());
} catch(\Exception $e) {
echo sprintf("StatusCode: %d\n", $e->getCode());
echo $e->getMessage() . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment