{"id":118,"date":"2025-08-25T09:23:41","date_gmt":"2025-08-25T01:23:41","guid":{"rendered":"https:\/\/www.whubellychen.xyz\/?p=118"},"modified":"2025-08-25T09:25:53","modified_gmt":"2025-08-25T01:25:53","slug":"google-gsutil-%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.whubellychen.xyz\/index.php\/2025\/08\/25\/google-gsutil-%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95\/","title":{"rendered":"Google gsutil \u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><strong>gsutil \u547d\u4ee4\u884c\u5de5\u5177\u5b89\u88c5\u3001\u914d\u7f6e\u4e0e\u4f7f\u7528\u8bf4\u660e<\/strong><\/p>\n<p>\u672c\u6587\u6863\u63d0\u4f9b gsutil \u547d\u4ee4\u884c\u5de5\u5177\u7684\u8be6\u7ec6\u64cd\u4f5c\u6307\u5357\uff0c\u6db5\u76d6\u4ece\u5b89\u88c5\u3001\u914d\u7f6e\u5230\u6570\u636e\u7ba1\u7406\u7684\u5168\u8fc7\u7a0b\u3002gsutil \u662f Google Cloud SDK \u7684\u6838\u5fc3\u7ec4\u4ef6\uff0c\u7528\u4e8e\u901a\u8fc7\u547d\u4ee4\u884c\u4e0e Google Cloud Storage \u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<h1><strong>1. \u5b89\u88c5 Google Cloud SDK (\u5305\u542b gsutil)<\/strong><\/h1>\n<p>\u901a\u8fc7\u5b98\u65b9 apt \u8f6f\u4ef6\u6e90\u8fdb\u884c\u5b89\u88c5\uff0c\u53ef\u786e\u4fdd\u540e\u7eed\u7684\u4fbf\u6377\u66f4\u65b0\u3002\u6b64\u6d41\u7a0b\u9002\u7528\u4e8e Ubuntu\/Debian \u53ca\u5176\u884d\u751f\u7cfb\u7edf\u3002<\/p>\n<h2>1.1 \u5b89\u88c5\u4f9d\u8d56\u5305<\/h2>\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u66f4\u65b0\u7cfb\u7edf\u8f6f\u4ef6\u5305\u5217\u8868\u5e76\u5b89\u88c5 HTTPS \u4f20\u8f93\u652f\u6301\u3002<\/p>\n<pre><code class=\"language-bash\">sudo apt update\nsudo apt install -y apt-transport-https ca-certificates gnupg curl<\/code><\/pre>\n<h2>1.2 \u5bfc\u5165 Google Cloud \u516c\u94a5<\/h2>\n<p>\u4e3a\u4fdd\u8bc1\u8f6f\u4ef6\u5305\u7684\u5b89\u5168\u6027\uff0c\u9700\u5bfc\u5165 Google Cloud \u7684 GPG \u5bc6\u94a5\u3002<\/p>\n<pre><code class=\"language-bash\">curl https:\/\/packages.cloud.google.com\/apt\/doc\/apt-key.gpg | sudo gpg --dearmor -o \/usr\/share\/keyrings\/cloud.google.gpg<\/code><\/pre>\n<h2>1.3 \u6dfb\u52a0 SDK \u8f6f\u4ef6\u6e90<\/h2>\n<p>\u5c06 Google Cloud SDK \u7684\u8f6f\u4ef6\u6e90\u5730\u5740\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u6e90\u5217\u8868\u4e2d\u3002<\/p>\n<pre><code class=\"language-bash\">echo \u201cdeb [signed-by=\/usr\/share\/keyrings\/cloud.google.gpg] https:\/\/packages.cloud.google.com\/apt cloud-sdk main\u201d | sudo tee -a \/etc\/apt\/sources.list.d\/google-cloud-sdk.list<\/code><\/pre>\n<h2>1.4 \u6267\u884c\u5b89\u88c5<\/h2>\n<p>\u66f4\u65b0\u8f6f\u4ef6\u5305\u5217\u8868\u4ee5\u5305\u542b\u65b0\u6dfb\u52a0\u7684\u6e90\uff0c\u5e76\u5b89\u88c5 Google Cloud SDK\u3002<\/p>\n<pre><code class=\"language-bash\">sudo apt update\nsudo apt install -y google-cloud-sdk<\/code><\/pre>\n<h1><strong>2. \u521d\u59cb\u5316\u4e0e\u8eab\u4efd\u9a8c\u8bc1<\/strong><\/h1>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u6267\u884c\u4e00\u6b21\u6027\u521d\u59cb\u5316\uff0c\u4ee5\u767b\u5f55 Google \u8d26\u6237\u5e76\u6388\u6743 SDK\u3002<\/p>\n<h2>2.1 \u6267\u884c\u521d\u59cb\u5316\u547d\u4ee4<\/h2>\n<p>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u914d\u7f6e\u6d41\u7a0b\u3002<\/p>\n<pre><code class=\"language-bash\">gcloud init<\/code><\/pre>\n<h2>2.2 \u5b8c\u6210\u8eab\u4efd\u9a8c\u8bc1<\/h2>\n<p>\u8be5\u547d\u4ee4\u5c06\u5f15\u5bfc\u5b8c\u6210\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u4e00\u4e2a Google \u8d26\u6237\u767b\u5f55\u9875\u9762\u3002<\/li>\n<li>\u9009\u62e9\u7528\u4e8e Google Cloud \u7684\u8d26\u6237\u5e76\u767b\u5f55\u3002<\/li>\n<li>\u6388\u6743 Google Cloud SDK \u8bbf\u95ee\u8d26\u6237\u6570\u636e\u3002<\/li>\n<li>\u6388\u6743\u6210\u529f\u540e\uff0c\u8ba4\u8bc1\u4fe1\u606f\u5c06\u81ea\u52a8\u4f20\u9012\u56de\u7ec8\u7aef\u3002<\/li>\n<\/ol>\n<p><em>\u6ce8\uff1a\u5728\u65e0\u56fe\u5f62\u754c\u9762\u7684\u670d\u52a1\u5668\u73af\u5883\u4e2d\uff0c\u53ef\u4f7f\u7528 gcloud auth login &#8211;no-launch-browser \u547d\u4ee4\uff0c\u901a\u8fc7\u590d\u5236\u7c98\u8d34\u94fe\u63a5\u548c\u6388\u6743\u7801\u7684\u65b9\u5f0f\u5b8c\u6210\u9a8c\u8bc1\u3002<\/em><\/p>\n<h1><strong>3. \u9879\u76ee\u914d\u7f6e (Config)<\/strong><\/h1>\n<p>gcloud \u547d\u4ee4\u884c\u5de5\u5177\u5728\u4e00\u4e2a\u6307\u5b9a\u7684\u201c\u9879\u76ee (Project)\u201d\u4e0a\u4e0b\u6587\u4e2d\u8fd0\u884c\u3002\u5fc5\u987b\u914d\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u9879\u76ee\u3002<\/p>\n<h2>3.1 \u5217\u51fa\u53ef\u7528\u9879\u76ee<\/h2>\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u67e5\u770b\u5f53\u524d\u8d26\u6237\u4e0b\u6240\u6709\u53ef\u7528\u7684\u9879\u76ee\u53ca\u5176 PROJECT_ID\u3002<\/p>\n<pre><code class=\"language-bash\">gcloud projects list<\/code><\/pre>\n<h2>3.2 \u8bbe\u7f6e\u9ed8\u8ba4\u9879\u76ee<\/h2>\n<p>\u4ece\u4e0a\u4e00\u6b65\u7684\u5217\u8868\u4e2d\uff0c\u9009\u62e9\u4e00\u4e2a PROJECT_ID\uff0c\u5e76\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u8bbe\u4e3a\u9ed8\u8ba4\u9879\u76ee\u3002<\/p>\n<pre><code class=\"language-bash\">gcloud config set project [PROJECT_ID]\n# *\u793a\u4f8b\uff1agcloud config set project ragbuild*<\/code><\/pre>\n<h1>3.3 \u9a8c\u8bc1\u5f53\u524d\u914d\u7f6e<\/h1>\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u68c0\u67e5\u5f53\u524d\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u786e\u4fdd project \u5b57\u6bb5\u5df2\u6b63\u786e\u8bbe\u7f6e\u3002<\/p>\n<pre><code class=\"language-bash\">gcloud config list<\/code><\/pre>\n<h1><strong>4. \u8bbf\u95ee\u7f51\u9875\u7248 Cloud Storage \u6d4f\u89c8\u5668<\/strong><\/h1>\n<p>Google Cloud \u63d0\u4f9b\u56fe\u5f62\u5316\u754c\u9762\u7528\u4e8e\u7ba1\u7406\u5b58\u50a8\u6876\u548c\u6570\u636e\u3002<\/p>\n<h2><strong>4.1 \u6253\u5f00\u65b9\u5f0f<\/strong><\/h2>\n<ul>\n<li><strong>\u76f4\u63a5\u94fe\u63a5<\/strong>: \u8bbf\u95ee <a href=\"https:\/\/console.cloud.google.com\/storage\/browser\">https:\/\/console.cloud.google.com\/storage\/browser<\/a><\/li>\n<li><strong>\u901a\u8fc7\u5bfc\u822a<\/strong>:\n<ol>\n<li>\u8bbf\u95ee Google Cloud Console \u4e3b\u9875 (<a href=\"https:\/\/console.cloud.google.com\/\">https:\/\/console.cloud.google.com\/<\/a>)\u3002<\/li>\n<li>\u70b9\u51fb\u5de6\u4e0a\u89d2\u7684\u5bfc\u822a\u83dc\u5355 (\u2630)\u3002<\/li>\n<li>\u5728\u83dc\u5355\u4e2d\u627e\u5230 \u201cCloud Storage\u201d \u5206\u7c7b\uff0c\u70b9\u51fb \u201c\u5b58\u50a8\u6876 (Buckets)\u201d\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>4.2 \u9879\u76ee\u9009\u62e9<\/h2>\n<p>\u8fdb\u5165\u7f51\u9875\u6d4f\u89c8\u5668\u540e\uff0c\u52a1\u5fc5\u786e\u8ba4\u9875\u9762\u9876\u90e8\u7684\u9879\u76ee\u9009\u62e9\u5668\u663e\u793a\u7684\u662f\u5f53\u524d\u9700\u8981\u64cd\u4f5c\u7684\u6b63\u786e\u9879\u76ee\u3002\u9879\u76ee\u4e0d\u5339\u914d\u662f\u5bfc\u81f4\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u5e38\u89c1\u539f\u56e0\u3002<\/p>\n<h1><strong>5. gsutil \u57fa\u672c\u6570\u636e\u64cd\u4f5c<\/strong><\/h1>\n<h2>5.1 \u521b\u5efa\u5b58\u50a8\u6876 (Bucket)<\/h2>\n<p>\u5b58\u50a8\u6876\u662f\u5b58\u653e\u6570\u636e\u7684\u9876\u7ea7\u5bb9\u5668\uff0c\u5176\u540d\u79f0\u5168\u5c40\u552f\u4e00\u3002<\/p>\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gcloud storage buckets create gs:\/\/[BUCKET_NAME] --project=[PROJECT_ID] --location=[LOCATION]\ngcloud storage buckets create gs:\/\/duojiechen-central-data-storage --project=ragbuild --location=asia-east1<\/code><\/pre>\n<h2><strong>5.2 \u4e0a\u4f20\u6570\u636e<\/strong><\/h2>\n<ul>\n<li><strong>\u4e0a\u4f20\u5355\u4e2a\u6587\u4ef6<\/strong>: \u4f7f\u7528 gsutil cp \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gsutil cp [LOCAL_FILE_PATH] gs:\/\/[BUCKET_NAME]\/[DESTINATION_PATH]\ngsutil cp .\/report.pdf gs:\/\/duojiechen-central-data-storage\/documents\/<\/code><\/pre>\n<\/li>\n<li><strong>\u540c\u6b65\u6574\u4e2a\u76ee\u5f55<\/strong>: \u4f7f\u7528 gsutil rsync \u547d\u4ee4\uff0c\u6b64\u65b9\u6cd5\u6548\u7387\u66f4\u9ad8\uff0c\u4ec5\u540c\u6b65\u5dee\u5f02\u6587\u4ef6\u3002\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gsutil -m rsync -r [LOCAL_DIRECTORY_PATH] gs:\/\/[BUCKET_NAME]\/[DESTINATION_DIRECTORY]\n# -m: \u5f00\u542f\u591a\u7ebf\u7a0b\uff0c\u63d0\u5347\u6027\u80fd\n# -r: \u9012\u5f52\u540c\u6b65\ngsutil -m rsync -r ~\/Projects\/Central_Data gs:\/\/duojiechen-central-data-storage\/Central_Data<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2><strong>5.3 \u4e0b\u8f7d\u6570\u636e<\/strong><\/h2>\n<ul>\n<li><strong>\u4e0b\u8f7d\u5355\u4e2a\u6587\u4ef6<\/strong>: \u4f7f\u7528 gsutil cp \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gsutil cp gs:\/\/[BUCKET_NAME]\/[OBJECT_PATH] [LOCAL_DESTINATION_PATH]\ngsutil cp gs:\/\/duojiechen-central-data-storage\/documents\/report.pdf .\/<\/code><\/pre>\n<\/li>\n<li><strong>\u540c\u6b65\u6574\u4e2a\u76ee\u5f55<\/strong>: \u4f7f\u7528 gsutil rsync \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gsutil -m rsync -r gs:\/\/[BUCKET_NAME]\/[SOURCE_DIRECTORY] [LOCAL_DESTINATION_DIRECTORY]\ngsutil -m rsync -r gs:\/\/duojiechen-central-data-storage\/Central_Data ~\/Backup\/Central_Data<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2><strong>5.4 \u6570\u636e\u7ba1\u7406<\/strong><\/h2>\n<ul>\n<li><strong>\u5217\u51fa\u5185\u5bb9 (List)<\/strong>: \u4f7f\u7528 gsutil ls \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u5217\u51fa\u6240\u6709\u5b58\u50a8\u6876\ngsutil ls\n\n# \u5217\u51fa\u7279\u5b9a\u5b58\u50a8\u6876\u5185\u7684\u5185\u5bb9\ngsutil ls -r gs:\/\/duojiechen-central-data-storage<\/code><\/pre>\n<\/li>\n<li><strong>\u79fb\u52a8\/\u91cd\u547d\u540d (Move\/Rename)<\/strong>: \u4f7f\u7528 gsutil mv \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u8bed\u6cd5: gsutil mv gs:\/\/[BUCKET_NAME]\/[SOURCE_OBJECT] gs:\/\/[BUCKET_NAME]\/[DESTINATION_OBJECT]\ngsutil mv gs:\/\/duojiechen-central-data-storage\/report.pdf gs:\/\/duojiechen-central-data-storage\/documents\/final_report.pdf<\/code><\/pre>\n<\/li>\n<li><strong>\u5220\u9664 (Remove)<\/strong>: \u4f7f\u7528 gsutil rm \u547d\u4ee4\u3002\n<pre><code class=\"language-bash\"># \u5220\u9664\u5355\u4e2a\u6587\u4ef6\ngsutil rm gs:\/\/duojiechen-central-data-storage\/documents\/final_report.pdf\n\n# \u9012\u5f52\u5220\u9664\u6574\u4e2a\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\ngsutil rm -r gs:\/\/duojiechen-central-data-storage\/old_data<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>gsutil \u547d\u4ee4\u884c\u5de5\u5177\u5b89\u88c5\u3001\u914d\u7f6e\u4e0e\u4f7f\u7528\u8bf4\u660e \u672c\u6587\u6863\u63d0\u4f9b gsutil \u547d\u4ee4\u884c\u5de5\u5177\u7684\u8be6\u7ec6\u64cd\u4f5c\u6307\u5357\uff0c\u6db5\u76d6\u4ece\u5b89\u88c5\u3001<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"https:\/\/www.whubellychen.xyz\/index.php\/2025\/08\/25\/google-gsutil-%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95\/\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[9],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-7","tag-linux"],"_links":{"self":[{"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":1,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whubellychen.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}