실행 중 GCS의 데이터 사용

12161 단어 GCSRuby

입문


이 글은 Linkbal_Advent_Calendar_2020 20일째 보도입니다.
오늘 우리는 루비에서 GCS(Google Cloud Storage)의 데이터에 대한 몇 가지 조작을 간단히 소개할 것이다.

실시


Gem 설정


우선 설치google-cloud-storagegem해야 합니다.최소한의 루비 버전은 2.4 이라는 것을 기억하세요.
방법 1:Gemfile의 파일에 다음 줄을 추가한 후 실행bundle install.
gem 'google-cloud-storage', '~> 1.29', '>= 1.29.2'
방법 2:
명령으로 직접 수행할 수 있습니다.
$ gem install google-cloud-storage

검증


사용하기 전에 검증 과정이 필요합니다.자격 증명에는 project_idcredentials 이 포함됩니다.
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!
    

    끝날 때


    여기서 마치겠습니다.만약 상술한 부분에 잘못된 점이 있다면 평론에서 의견을 얻을 수 있다면 좋겠다.기사를 열람해 주셔서 대단히 감사합니다.
    다음 기사.

    참고 자료

    좋은 웹페이지 즐겨찾기