how to upload multiple files like a bulk insert on

2019-08-23 07:51发布

问题:

I have the following situation. I'm using laravel and google cloud storage so basically, I have my own custom filesystem attached to google cloud storage. My code looks like this -

try{
    $disk = Storage::disk('gcs');

                $disk->put('/service_provider/transports/tech_passports', $file1);
                $disk->put('/service_provider/transports/tech_passports', $file2);

                $disk->put('/service_provider/transports/pictures', $file3);
                $disk->put('/service_provider/transports/pictures', $file4);
                $disk->put('/service_provider/transports/pictures', $file5);
                $disk->put('/service_provider/transports/pictures', $file6);
    } catch(\Exception $e){

}

Now i'm worried. what if the first three files get uploaded and the fourth one has an error. so it will go to catch block and finally, only 3 files will be uploaded which is not what I need.

What I need is all get uploaded or no file gets uploaded just like atomicity. how do I achieve that?

回答1:

To be sure that all your files get uploaded, try using resumable uploads on each of your individual files, with the following steps:

Initiate upload > Process response > Upload The file > Check Upload Status > Process response > Resume the upload

Retry last three steps to resume upload as many times as necessary or implement another solution (for example: delete all files already uploaded), if it is what you need in your application.

You can also check Google Cloud Storage API PHP resumable behavior in the example from another question on StackOverflow.

Please note that also Cloud SDK utility Gsutil has resumable upload implemented in it. It can be used to Synchronize content of two buckets/directories.