如何在谷歌云存储中存储同名文件 [英] How to store same name file in google cloud storage

查看:109
本文介绍了如何在谷歌云存储中存储同名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站,该网站上有许多用户上传的图像文件,因此随着时间的推移,链接可能会在Google云端存储中出现相同名称的图像,即使一个用户可以随时间上传相同名称的图像,那么如何我可以解决这个问题吗?

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 user can upload same name image over the time, So how can I solve this issue?

推荐答案

您可以做的一件事就是在上传时在文件名中添加时间戳记

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

例如

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

然后,当您允许用户下载文件时,请读取文件名并从下划线删除最后一部分,并在带有原始最终名称的url中为该参数提供用户链接

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"

这篇关于如何在谷歌云存储中存储同名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆