실행 중 GCS의 데이터 사용
입문
이 글은 Linkbal_Advent_Calendar_2020 20일째 보도입니다.
오늘 우리는 루비에서 GCS(Google Cloud Storage)의 데이터에 대한 몇 가지 조작을 간단히 소개할 것이다.
실시
Gem 설정
우선 설치google-cloud-storage
의gem
해야 합니다.최소한의 루비 버전은 2.4
이라는 것을 기억하세요.
방법 1:Gemfile
의 파일에 다음 줄을 추가한 후 실행bundle install
.gem 'google-cloud-storage', '~> 1.29', '>= 1.29.2'
방법 2:
명령으로 직접 수행할 수 있습니다.$ gem install google-cloud-storage
검증
사용하기 전에 검증 과정이 필요합니다.자격 증명에는 project_id
및 credentials
이 포함됩니다.require "google/cloud/storage"
storage = Google::Cloud::Storage.new(
project_id: "my-project",
credentials: "/path/to/keyfile.json"
)
Gem 설정
우선 설치
google-cloud-storage
의gem
해야 합니다.최소한의 루비 버전은 2.4
이라는 것을 기억하세요.방법 1:
Gemfile
의 파일에 다음 줄을 추가한 후 실행bundle install
.gem 'google-cloud-storage', '~> 1.29', '>= 1.29.2'
방법 2:명령으로 직접 수행할 수 있습니다.
$ gem install google-cloud-storage
검증
사용하기 전에 검증 과정이 필요합니다.자격 증명에는
project_id
및 credentials
이 포함됩니다.require "google/cloud/storage"
storage = Google::Cloud::Storage.new(
project_id: "my-project",
credentials: "/path/to/keyfile.json"
)
project_id
는 프로젝트의 표식이다.credentials
는 인증 정보를 저장하는 JSON 파일의 경로입니다.일반
API Manager
을 선택한 다음Credentials
을 클릭합니다.그런 다음 Add credentials
선택 상자를 찾아 Service account
를 선택합니다.생성된 후 새 키 파일Json
을 다운로드합니다.경로를 위치에 저장하고 자격 증명 섹션에 기록합니다.또한 GCP(Google Cloud Platform)에서 credentials 경로를 가져오는 방법:
IAM&Admin => Service Accounts
에서 새로운Service Account
을 만들고, 새로운Keys
을 만들고, Json
형식으로 다운로드합니다.그것Keys
은 통에 접근하는 데 쓰인다.다운로드한 후 어딘가에 놓고 그 파일의 경로를 기입하세요.https://googleapis.dev/ruby/google-cloud-storage/latest/file.AUTHENTICATION.html
데이터 조작
삽질
시간대는 데이터의 용기다.프로젝트에서 만들 수 있는 시간 수는 제한되지 않습니다.사용 시간은 데이터에 대한 접근을 조직하고 제어할 수 있다.
지정된 기간 동안 읽어들일 때
storage.bucket "bucket_name"
의 문을 사용합니다.예:
require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
프로젝트에서 통째로 들고 싶은 명절에.buckets = storage.buckets
API 호출 수를 제한하려는 경우buckets = storage.buckets
buckets.all(request_limit: 10) do |bucket|
# do something
end
또 새로운 통을 만들기 위해 사용한다storage.create_bucket "bucket_name"
.예:
bucket = storage.create_bucket "my_new_bucket"
문서 (건)파일은 구글 클라우드 저장소에 저장된 단일 데이터 대상이다.
시간대에 지정된 파일을 읽어들일 때 파일 이름이 필요합니다.
require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"
통에서 모든 서류를 찾으려는 명절에all_files = bucket.files
또는 지정된 경로의 모든 파일을 읽어들일 수 있습니다.all_files = bucket.files prefix: "avatars/"
또한 시간대에 새 파일을 만들 때 로컬에 저장된 파일의 경로를 지정해야 합니다.require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
bucket.create_file "/tmp/images/avatars/image1.png",
"avatars/image1.png"
파일 다운로드파일을 다운로드할 때 사용
file.download
.require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"
file.download "/tmp/images/avatars/image1.png" # path to save file at local
StringIO 객체도 다운로드할 수 있습니다.require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
file = bucket.file "hello_world.txt"
downloaded = file.download
downloaded.rewind
downloaded.read # => "Hello World!"
클라이언트가 인증하지 않은 공개 파일을 다운로드하려면 skip_lookup: true
를 사용하십시오.require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "public_bucket", skip_lookup: true
file = bucket.file "path/to/public_file.ext", skip_lookup: true
downloaded = file.download
액세스 제어require "google/cloud/storage"
storage = Google::Cloud::Storage.new
bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"
email = "[email protected]"
사용자에게 통이나 파일에 대한 액세스를 허용하거나 제한할 수 있습니다.bucket.acl.add_reader "user-#{email}"
file.acl.add_reader "user-#{email}"
마찬가지로 사용자는 시간대나 파일에 대한 접근을 허용하거나 제한할 수 있다.bucket.acl.add_reader "group-#{email}"
file.acl.add_reader "group-#{email}"
또한 기간과 파일에 대한 액세스에 대해 미리 정의된 권한 목록을 부여할 수 있습니다.bucket.acl.public!
file.acl.public!
끝날 때
여기서 마치겠습니다.만약 상술한 부분에 잘못된 점이 있다면 평론에서 의견을 얻을 수 있다면 좋겠다.기사를 열람해 주셔서 대단히 감사합니다.
다음 기사.
참고 자료
Reference
이 문제에 관하여(실행 중 GCS의 데이터 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/duong_nguyen/items/adb287d4601fcd2f033c
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(실행 중 GCS의 데이터 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/duong_nguyen/items/adb287d4601fcd2f033c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)