How to store same name file in google cloud storage

后端 未结 1 1205
醉话见心
醉话见心 2021-01-20 06:29

I am creating a website which has lots of user uploaded image files, So with the time it may happen link there will be same name images on Google cloud storage, Even one use

相关标签:
1条回答
  • 2021-01-20 06:44

    One thing you can do is add timestamp to your filename while uploading

    e.g.

    $date = new DateTime();
    $timeStamp = $date->getTimestamp();
    $fileName = $fileName. "_" . $timeStamp;
    

    And then when you are allowing user to download the file, read the file name and remove last part from underscore and give user link with this parameter in url with original final name

    $service = new Google_Service_Storage($client);
    $objects = $service->objects->listObjects($bucketName);
    
    // Assuming first item you are downloading
    $objects = $objects['modelData']["items"][0];
    $filename = substr($objects["name"], 0, strrchr($objects["name"],"_"));
    $url .= "&response-content-disposition=attachment;%20filename=.$filename"
    
    0 讨论(0)
提交回复
热议问题