[Google :: Apis :: DriveV3] Получить URL загруженного файла - PullRequest
0 голосов
/ 14 апреля 2020

Я хотел бы получить URL-адрес, загруженный на Google Drive с помощью гема google_drive в рельсах.

Я пытался получить его из созданного возвращаемого значения, но такой ссылки не было, кроме ссылки API, поэтому вы невозможно получить доступ к Google Диску.

Есть ли способ получить эту ссылку?

Это возвращенная ссылка, а не эта

https://www.googleapis.com/drive/v3/files/1m4Ja7qCNrfqhrkrtKibuPoV02Shp5w7G2?

Я хочу это

https://drive.google.com/drive/u/0/folders/1ObHbN1heqwewgx1RkyEJ1Bm4GL3TSTjmI

Спасибо.

require "google/apis/drive_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"

OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "upload-image-to-gdrive".freeze
CREDENTIALS_PATH = "credentials.json".freeze
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE


class DriveUploader
  def initialize
    # Initialize the API
    @@drive = Google::Apis::DriveV3
    @@service = @@drive::DriveService.new
    @@service.client_options.application_name = APPLICATION_NAME
    @@service.authorization = authorize
  end

  def authorize
    client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
    token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
    authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
    user_id = "default"
    credentials = authorizer.get_credentials user_id
    credentials
  end

  def upload(name, image_source)
    file_metadata = {
        name: name,
        parents: ['1ObHbN11jdig94x1RkyEJ1Bm4GL3TSTjmI'] # Drive ID
    }
    file = @@service.create_file(
        file_metadata,
        fields: 'id',
        upload_source: image_source,
        #content_type: 'image/jpeg' # 'application/pdf'
        content_type: 'application/pdf' # 'application/pdf'
    )
    user_permission = {type: 'anyone', role: 'reader'}
    @@service.create_permission(file.id, user_permission, fields: 'id')
    file
  end
end

1 Ответ

1 голос
/ 14 апреля 2020
  • После загрузки файла на диск и получения его идентификатора вы можете использовать метод Files: get , чтобы получить file resource.

  • Файловый ресурс содержит все метаданные файла, как указано в документации

  • Полезными для вас могут быть, например, webContentLink или webViewLink

  • Оба можно получить, указав их в fields для метода drive.files.get
...