Skip to content

Instantly share code, notes, and snippets.

@vishwarajanand
Created February 26, 2024 22:38
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 vishwarajanand/d6d44477f382f7ba92778d6ed149d991 to your computer and use it in GitHub Desktop.
Save vishwarajanand/d6d44477f382f7ba92778d6ed149d991 to your computer and use it in GitHub Desktop.
Testing Google Cloud Storage Resumable Uploader headers
<?php
// Ref: https://github.com/googleapis/google-cloud-php/issues/7088
require __DIR__ . "/vendor/autoload.php";
use Google\Cloud\Storage\StorageClient;
$gcsClient = new StorageClient([
"projectId" => "XXXXXXX",
"requestTimeout" => 10,
]);
$gcsBucket = $gcsClient->bucket("XXXXXXX");
$uploader = $gcsBucket->getResumableUploader(
fopen(__DIR__ . '/lorem.txt', 'r'), # Cmd to create file `perl -le 'print "xyz" x 10485760' > lorem.txt`
["name" => "test/issue_7088"]
);
try {
$objectData = $uploader->upload();
} catch (\Exception $ex) {
if (stripos(
$ex->getMessage(),
"Upload failed. Please use this URI to resume your upload:"
) === false
) {
throw $ex;
}
echo("Network error while uploading file to bucket, trying to reconnect and continue uploading".PHP_EOL);
$stop = false;
$count = 0;
while (!$stop) {
++$count;
if ($count >= 10) {
throw new \Exception("Upload file to bucket failed after 100 tries");
}
try {
$resumeUri = $uploader->getResumeUri();
echo("Resuming".PHP_EOL);
$objectData = $uploader->resume($resumeUri);
$stop = true;
} catch (\Exception $ex) {
if (stripos(
$ex->getMessage(),
"Upload failed. Please use this URI to resume your upload:"
) === false
) {
throw $ex;
}
// ignore error and continue
sleep(5);
}
}
}
echo "Uploaded " . JSON_ENCODE($objectData, JSON_PRETTY_PRINT) . PHP_EOL;
➜ Storage git:(fix_resumable_uploader) ✗ php issue_7088.php
Network error while uploading file to bucket, trying to reconnect and continue uploading
Resuming
response body size is zero
rangeStart value is bytes=0-216793087
Resuming
response body size is zero
rangeStart value is bytes=0-438829055
rangeStart value is null
Uploaded {
"kind": "storage#object",
"id": "gcloXXXXXXa8\/test\/issue_7088\/1708986505212283",
"selfLink": "https:\/\/www.googleapis.com\/storage\/v1\/b\/gcloXXXXXXa8\/o\/test%2Fissue_7088",
"mediaLink": "https:\/\/storage.googleapis.com\/download\/storage\/v1\/b\/gcloXXXXa8\/o\/test%2Fissue_7088?generation=1708986505212283&alt=media",
"name": "test\/issue_7088",
"bucket": "gcloXXXXea8",
"generation": "1708986505212283",
"metageneration": "1",
"contentType": "application\/octet-stream",
"storageClass": "STANDARD",
"size": "601457281",
"md5Hash": "Szwlox2XSBCDj1PSp2r0jQ==",
"crc32c": "o5mfPw==",
"etag": "CPvixuyGyoQDEAE=",
"timeCreated": "2024-02-26T22:28:25.225Z",
"updated": "2024-02-26T22:28:25.225Z",
"timeStorageClassUpdated": "2024-02-26T22:28:25.225Z"
}
➜ Storage git:(fix_resumable_uploader) ✗
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment