LoginSignup
8
6

More than 3 years have passed since last update.

google driveとAWS S3間にファイルの転送(リモートマシーン)

Posted at

AWS S3でデータを保存・管理していますが、中身の確認をDirveでやった方がやりやすい場合がありますので、ファイルの転送(もしくは同期化)ができれば色々な使い道があると思って、試してみたことをメモしました。

やりたいこと

データをS3、Google drive間の相互転送

結論

rcloneを利用して、S3、Google drive間の相互転送ができました。

準備するもの

  • マシーン:AWS EC2・Amazon Linux2
  • AWS S3とS3の操作ができるcredentials情報
  • Google drive
  • GCP Google Drive APIを利用できるプロジェクトと関連するサービスアカウント
  • rclone

rclone

公式サイトの内容に従って導入する。
Linuxなので、下記のコマンドでインストールする。

curl https://rclone.org/install.sh | sudo bash

GCPのプロジェクトとサービスアカウント

GCP Google Drive APIを利用できるプロジェクトをまず準備してください。
普通にGCPのプロジェクトの作成なので、流れのみ記載しますが、詳細を割愛します。

  • GCPのプロジェクトの作成
  • Google drive APIを有効にする

個人アカウントと関連したくないため、OAuthクライエントIDではなく、サービス アカウントを作成して、サービス アカウントのJsonファイルをリモート環境にアップロードします。
今回の場合は/home/ec2-user/gcloud/xxxxxx.jsonに配置しました。

AWS credentials

今回の場合、AWSのEC2の中にはIAMで作成したユーザーアカウントのcredentials情報が入っていて、このアカウントにもS3の操作権限付いていますので、そのままで使う。(もしなければ、新規ユーザーアカウントを作成して使う。)

/.aws/credentails
[default]
aws_access_key_id = ************
aws_secret_access_key = ****************

rcloudの環境設定

rcloneの設定コマンドを実行します。

rclone config

まずはGoogle Driveの設定をします。

No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config

n/s/q> n
name> gdrive

nを入力して、名前をgdriveにしました。

その後に、storageタイプの一覧が表示されます。
Google Driveの設定なので、drive(数字の13でもOK)を入力する。

Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
...
13 / Google Drive
   \ "drive"
...
Storage> drive

サービスアカウントを使いますので、client_idclient_secretは未入力のままでEnterを押します。

client_id>
client_secret>

Scopeの部分は、とりあえずフルアクセス権限にするので、1を入力します。

Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1

root_folderを設定したい場合、適切のroot_folder_idを入力します。なければ、未入力のままでEnterします。

Enter a string value. Press Enter for the default ("").
root_folder_id>

サービスアカウントの設定には、GCPのプロジェクトとサービスアカウントの時に配置したJsonファイルのパス(/home/ec2-user/gcloud/xxxxxx.json)を入力する。

Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> /home/ec2-user/gcloud/xxxxxx.json

advanced configとRemote configの部分は、未入力のままでEnterを押します。
今回はチームドライブを使用しますので、yを入力します。

Edit advanced config? (y/n)
y) Yes
n) No
y/n> n
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n
Configure this as a team drive?
y) Yes
n) No
y/n> y

続いて、内容を確認して、OKでしたら、yを入力します。
これでGoogle Driveの設定を完了します。

--------------------
[gdrive]
client_id = 
client_secret = 
scope = drive
root_folder_id = 
service_account_file = /home/ec2-user/gcloud/xxxxx.json
team_drive = xxxxxxxxx
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

次はS3の設定をします。設定流れは大体一緒なので、重要な部分のみ記載します。
細かいの部分はこちらを参照すればOKだと思います。


name> awsS3

Storage> s3

provider> 1

env_auth> 2

region> 12

location_constraint> 12

acl> 

server_side_encryption> 2
sse_kms_key_id>

storage_class> 1

rcloneの使用

ファイル一覧:ls, lsd

ls:指定しているディレクトリのファイルを全てリスト表示する

rclone ls gdrive:path/to/directory

lsd:配下の下一階層のファイルのみをリスト表示する。

rclone lsd gdrive:path/to/directory

ファイルのコピー:copy

Google driveとAWS S3間のファイル転送する。

# Google driveからAWS S3にファイルをコピーする。
rclone -P copy gdrive:path/to/directory awsS3:/path/to/directory

# AWS S3からGoogle driveにファイルをコピーする。
rclone -P copy awsS3:/path/to/directory gdrive:path/to/directory 

-Pをつけると進捗を表示してくれます。

ファイルの削除:delete、deleteFile

# ディレクトリ配下のファイルの全削除(ディレクトリを削除しません)
rclone delete gdrive:path/to/directory

# 単一ファイルの削除
rclone deletefile gdrive:path/to/file

フォルダーの削除:purge

rclone purge gdrive:path/to/directory

以上はrcloneを試してみた結果です。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6