通过服务器将文件传输到google cloud

项目工程化时发生了问题,属于google cloud的相关运用,由于项目之前在服务器中已经配置好了,并且这个项目五十多g,所以就在找一种方法能够直接上传到google cloud中。于是选择了google cloud中的cloud storage进行大文件的存储。下面展示如何在服务器中直接将数据迁移到cloud storage中。

1. 配置python环境

参考:https://cloud.google.com/python/docs/reference/storage/latest

# Mac / Linux
python3 -m venv <your-env>
source <your-env>/bin/activate
pip install google-cloud-storage
# 视窗
py -m venv <your-env>
.\<your-env>\Scripts\activate
pip install google-cloud-storage

2. 安装gcloud cli

参考 https://cloud.google.com/sdk/docs/install?hl=zh-cn#linux

# 1.下载客户端
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-linux-x86_64.tar.gz

# 2. 解压客户端
tar -xf google-cloud-cli-linux-x86_64.tar.gz

# 3. 安装客户端
./google-cloud-sdk/install.sh

# 4. 初始化,这个过程会让进行浏览器登录信息认证
./google-cloud-sdk/bin/gcloud init

3. 使用 Google 账号配置 ADC

登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。参考:https://cloud.google.com/docs/authentication/provide-credentials-adc?hl=zh-cn#local-dev

gcloud auth application-default login

4. 测试下python是否可以连接

参考:https://cloud.google.com/docs/authentication/client-libraries?hl=zh-cn


from google.cloud import storage


def authenticate_implicit_with_adc(project_id="your-google-cloud-project-id"):
    """
    When interacting with Google Cloud Client libraries, the library can auto-detect the
    credentials to use.

    // TODO(Developer):
    //  1. Before running this sample,
    //  set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc
    //  2. Replace the project variable.
    //  3. Make sure that the user account or service account that you are using
    //  has the required permissions. For this sample, you must have "storage.buckets.list".
    Args:
        project_id: The project id of your Google Cloud project.
    """

    # This snippet demonstrates how to list buckets.
    # *NOTE*: Replace the client created below with the client required for your application.
    # Note that the credentials are not specified when constructing the client.
    # Hence, the client library will look for credentials using ADC.
    storage_client = storage.Client(project=project_id)
    buckets = storage_client.list_buckets()
    print("Buckets:")
    for bucket in buckets:
        print(bucket.name)
    print("Listed all storage buckets.")

成功后可以实际看到能够输出的具体块存储都有哪些。

5. 正式开始使用

api 参考:https://github.com/googleapis/python-storage/tree/main/samples#upload-file

# 1.下载代码信息
git clone https://github.com/googleapis/python-storage.git
# 2. 安装依赖
cd samples/snippets
pip install -r requirements.txt
# 3. 文件传输
python storage_upload_from_memory.py <BUCKET_NAME> <CONTENTS> <DESTINATION_BLOB_NAME>

但是没有发现文件夹传输。这里有几种方法推荐

方法一:写一个脚本,遍历所有文件逐一推送。

基于文件传输,自己写了一个文件夹传输的脚本。
一定要注意:下面这个脚本最好在samples/snippets的路径下创建,这样直接启用就行了。

import glob
import os
import subprocess
def traverse_files(folder_path):
    list_result = []
    file_paths = glob.glob(os.path.join(folder_path, '**'), recursive=True)
    for file_path in file_paths:
        if os.path.isfile(file_path):
            # 对文件进行处理
            print(file_path)
            list_result.append(file_path)
    return list_result
if __name__ == "__main__":
    list_infomation = traverse_files('/root/Moore-AnimateAnyone')
    for file in list_infomation:
        subprocess.run('python storage_upload_file.py pose_infomation {0} {0}'.format(file), shell=True)

看下效果。在这里插入图片描述

方法二:打压缩包,使用压缩包传送

  1. 介绍linux中能够进行压缩的方法
#方法一:使用tar命令进行压缩

tar -czvf compressed.tar.gz folder/   # 压缩为tar.gz格式
tar -cjvf compressed.tar.bz2 folder/  # 压缩为tar.bz2格式
tar -cJvf compressed.tar.xz folder/   # 压缩为tar.xz格式
#这些命令将文件夹"folder"压缩成一个具有相应格式的压缩文件。

#方法二:使用zip命令进行压缩

zip -r compressed.zip folder/  # 压缩为zip格式
#该命令将文件夹"folder"压缩成一个zip文件。

#方法三:使用7zip命令进行压缩

#首先,确保已安装7zip工具。然后使用以下命令:

7z a compressed.7z folder/   # 压缩为7z格式
#该命令将文件夹"folder"压缩成一个7z格式的压缩文件。

#这些是在Linux上压缩文件夹的几种不同的实现方法。
  1. 压缩包传输
    仿照上述文件传输即可
# 文件传输
python storage_upload_from_memory.py <BUCKET_NAME> <CONTENTS> <DESTINATION_BLOB_NAME>

方法三:cyberduck传输

推荐一个工具,可以利用cyberduck进行传输,但是弊端好像都要先把文件拉到本地,再推到其他地方。
在这里插入图片描述
压缩包,使用压缩包传送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值