JSn*_*now 5 google-cloud-storage google-cloud-platform
我是Google Cloud Platform的新手.我已经在datalab上训练了我的模型,并将模型文件夹保存在我的存储桶中的云存储中.通过右键单击文件 - >另存为链接,我可以将存储桶中的现有文件下载到本地计算机.但是当我尝试按照上面相同的步骤下载文件夹时,我没有得到文件夹,而是它的图像.无论如何我可以下载整个文件夹及其内容吗?是否有任何gsutil命令将文件夹从云存储复制到本地目录?
njm*_*was 17
这是从 Google Cloud Storage Bucket 下载文件夹的方法
运行以下命令将其从存储桶存储下载到您的 Google Cloud Console 本地路径
gsutil -m cp -r gs://{bucketname}/{folderPath} {localpath}
运行该命令后,通过运行ls命令列出本地路径上的文件和目录来确认您的文件夹位于本地路径上
现在通过运行以下命令来压缩您的文件夹
zip -r foldername.zp yourfolder/*
Run Code Online (Sandbox Code Playgroud)
压缩过程完成后,单击 Google Cloud Console 右侧的更多下拉菜单,
然后选择“下载文件”选项。系统将提示您输入要下载的文件的名称,输入 zip 文件的名称 - “foldername.zp”
你可以找到gsutil工具文档在这里和你的问题更具体的在这里.
您要使用的命令是:
gsutil cp -r gs://bucket/folder .
Run Code Online (Sandbox Code Playgroud)
先决条件:Google Cloud SDK 已安装并初始化($ glcoud init)
命令:
gsutil -m cp -r gs://bucket-name .Run Code Online (Sandbox Code Playgroud)
这将使用速度更快的多线程复制所有文件。我发现官方 Gsutil 文档中指示使用的“dir”命令不起作用。
如果您使用 python 从谷歌云存储下载数据并希望保持相同的文件夹结构,请按照我在 python 中编写的代码进行操作。
选项1
from google.cloud import storage
def findOccurrences(s, ch): # to find position of '/' in blob path ,used to create folders in local storage
return [i for i, letter in enumerate(s) if letter == ch]
def download_from_bucket(bucket_name, blob_path, local_path):
# Create this folder locally
if not os.path.exists(local_path):
os.makedirs(local_path)
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs=list(bucket.list_blobs(prefix=blob_path))
startloc = 0
for blob in blobs:
startloc = 0
folderloc = findOccurrences(blob.name.replace(blob_path, ''), '/')
if(not blob.name.endswith("/")):
if(blob.name.replace(blob_path, '').find("/") == -1):
downloadpath=local_path + '/' + blob.name.replace(blob_path, '')
logging.info(downloadpath)
blob.download_to_filename(downloadpath)
else:
for folder in folderloc:
if not os.path.exists(local_path + '/' + blob.name.replace(blob_path, '')[startloc:folder]):
create_folder=local_path + '/' +blob.name.replace(blob_path, '')[0:startloc]+ '/' +blob.name.replace(blob_path, '')[startloc:folder]
startloc = folder + 1
os.makedirs(create_folder)
downloadpath=local_path + '/' + blob.name.replace(blob_path, '')
blob.download_to_filename(downloadpath)
logging.info(blob.name.replace(blob_path, '')[0:blob.name.replace(blob_path, '').find("/")])
logging.info('Blob {} downloaded to {}.'.format(blob_path, local_path))
bucket_name = 'google-cloud-storage-bucket-name' # do not use gs://
blob_path = 'training/data' # blob path in bucket where data is stored
local_dir = 'local-folder name' #trainingData folder in local
download_from_bucket(bucket_name, blob_path, local_dir)
Run Code Online (Sandbox Code Playgroud)
选项 2:使用 gsutil sdk 下面是通过 python 程序执行此操作的另一种选择。
def download_bucket_objects(bucket_name, blob_path, local_path):
# blob path is bucket folder name
command = "gsutil cp -r gs://{bucketname}/{blobpath} {localpath}".format(bucketname = bucket_name, blobpath = blob_path, localpath = local_path)
os.system(command)
return command
Run Code Online (Sandbox Code Playgroud)
选项 3 - 没有 python,直接使用终端和谷歌 SDK 先决条件:谷歌云 SDK 已安装并初始化($ glcoud init)请参阅以下链接以获取命令:
https://cloud.google.com/storage/docs/gsutil/commands/cp
| 归档时间: |
|
| 查看次数: |
6231 次 |
| 最近记录: |