7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Cloud Storage に S3 互換の REST でアップロード

Posted at

先ほど次のような記事を書きましたが、

Google Cloud Storage に AWS CLI や AWS SDK for PHP でアップロード

要するに Interoperability API を有効にすれば S3 互換の REST API が有効になるってことなので、普通に curl で REST API を叩いてアップロードしてみます。

<?php
$accessKey = 'YOUR_ACCESS_KEY';
$secretAccessKey = 'YOUR_SECRET_ACCESS_KEY';
$bucket = 'YOUR_BUCKET_NAME';

$path = 'rest.txt';
$type = 'text/plain';
$body = 'this is rest api';

$verb = 'PUT';
$md5 = base64_encode(md5($body, true));
$date = gmdate(\DateTime::RFC2822);
$amzHeaders = '';
$resource = "/$bucket/$path";

$stringToSign = implode("\n", [$verb, $md5, $type, $date, $amzHeaders . $resource]);
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $secretAccessKey, true));
$authorization = sprintf('AWS %s:%s', $accessKey, $signature);

$headers = [
    "Date: $date",
    "Content-Type: $type",
    "Content-MD5: $md5",
    "Authorization: $authorization",
];

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => "https://storage.googleapis.com/$bucket/$path",
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_HEADER => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $body,
]);

curl_exec($ch);

実行します。

$ php google-cloud-storage-rest.php

デベロッパーコンソールのストレージブラウザを見てみます。

10-rest.png

アップロードされています。

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?