Skip to content

Instantly share code, notes, and snippets.

@kaz29

kaz29/composer.json

Created Nov 1, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.