如何在不保存本地的情况下将数据上传到Google存储空间

时间:2019-10-29 14:09:46

标签: elixir google-cloud-storage

此代码对我来说效果很好:

GCloudex.CloudStorage.Client.put_object_content("my-bucket", "my_file_name", "heloo")

我现在正在尝试使用摇摇欲坠的库google_api_storage,这也是我尝试过的:

  @spec upload_file(bucket_id, file_path, name) :: {:ok, media_link} | {:error, error_message}
  def upload_file(bucket_id, file_path, name) do
    # Make the API request.
    res = GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple(
                    Connection.get(), bucket_id, "multipart",
                    %{name: Path.basename(name)}, file_path)
    case res do
      {:ok, %GoogleApi.Storage.V1.Model.Object{mediaLink: media_link}} -> {:ok, media_link}
      {:error, %Tesla.Env{body: body}} -> {:error, body}
      _ -> {:error, "unkonw error"}
    end

  end

我想上传而不在本地保存,我将使用什么功能以及如何使用?
storage_objects_insert-我看不到可以在请求中插入内容正文的位置吗?


通常,API中是否有任何引用可以将数据上传到google docs中的存储中?

2 个答案:

答案 0 :(得分:1)

使用Google Cloud Storage API,您可以直接在POST请求的正文中提供文件数据。您可以找到Cloud Storage对象插入API参考here

答案 1 :(得分:0)

有关卷曲请求,请参见here

curl -k -v -X POST \
-H "Authorization: Bearer <your_oauth2_token>" -H "Content-Length: 8" \
-H "Content-Type: text/plain" \
'https://www.googleapis.com/upload/storage/v1/b/your-bucket/o?uploadType=media&name=yourobjectname' \
-d 'yourdata'

使用该库,使用最新版本{:google_api_storage, "~> 0.13.0"}

conn = GoogleApi.Storage.V1.Connection.new("TOKEN")

{:ok, object} =
  GoogleApi.Storage.V1.Api.Objects.storage_objects_insert(
    conn,
    "BUCKET_NAME",
    body: "Hello, World! This is the content of the file.",
    name: "file.txt"
  )

请参阅https://github.com/googleapis/elixir-google-api/issues/1197