View Source COS.Object (tencent_cloud_cos v0.1.1)

对象(Object)是对象存储的基本单元,可理解为任何格式类型的数据,例如图片、文档和音视频文件等。

腾讯云文档

Link to this section Summary

Link to this section Functions

Link to this function

append(host, key, position, content, opts \\ [])

View Source
@spec append(
  host :: binary(),
  key :: binary(),
  position :: pos_integer(),
  content :: binary(),
  opts :: [headers: Tesla.Env.headers(), tesla_opts: Tesla.Env.opts()]
) :: Tesla.Env.t()

追加上传对象 - 腾讯云文档

示例

示例

iex> COS.Object.append("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", 0, "hello")
{:ok, %Tesla.Env{
  body: "",
  headers: [
    {"x-cos-content-sha1", "5d41402abc4b2a76b9719d911017c592"},
    {"x-cos-next-append-position", "5"},
    ...
  ],
  ...
}}

iex> COS.Object.append("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", 5, " world")
{:ok, %Tesla.Env{
  body: "",
  headers: [
    {"x-cos-content-sha1", "b7913aa15c43be7d534b4eec6e99e8a0"},
    {"x-cos-next-append-position", "11"},
    ...
  ],
  ...
}}
Link to this function

copy(host, key, source, opts \\ [])

View Source
@spec copy(
  host :: binary(),
  key :: binary(),
  source :: binary(),
  opts :: [headers: Tesla.Env.headers(), tesla_opts: Tesla.Env.opts()]
) :: Tesla.Env.t()

复制对象 - 腾讯云文档

创建一个已存在 COS 的对象的副本,即将一个对象从源路径(对象键)复制到目标路径(对象键)。 建议对象大小为1M到5G,超过5G的对象请使用分块上传(Todo)。

示例

示例

iex> COS.Object.copy(
       "https://bucket-1250000000.cos.ap-beijing.myqcloud.com",
       "destination.txt",
       "other-bucket-1250000000.cos.ap-shanghai.myqcloud.com/source.txt"
     )
{:ok, %Tesla.Env{
  body: %{
    "crc64" => "1791993320000000000",
    "e_tag" => ""6ae33dfd6c0c9fa03b77f75xxxxxxxxx"",
    "last_modified" => "2022-03-29T17:20:24Z"
  },
  headers: [
    {"server", "tencent-cos"},
    {"data", "Tue, 29 Mar 2022 16:39:58 GMT"},
    ...
  ],
  ...
}}
Link to this function

delete(host, key, opts \\ [])

View Source
@spec delete(
  host :: binary(),
  key :: binary(),
  opts :: [
    query: %{optional(:version_id) => binary()} | nil,
    tesla_opts: Tesla.Env.opts()
  ]
) :: Tesla.Env.t()

删除单个对象 - 腾讯云文档

示例

示例

COS.Object.delete("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt")

# 删除“文件夹”,如果“文件夹”内有对象,则不会删除。
COS.Object.delete("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example/")
Link to this function

exists?(host, key, opts \\ [])

View Source
@spec exists?(
  host :: binary(),
  key :: binary(),
  opts :: [
    query: %{optional(:version_id) => binary()} | nil,
    headers: Tesla.Env.headers(),
    tesla_opts: Tesla.Env.opts()
  ]
) :: boolean()

检查对象是否存在

示例

示例

iex> COS.Object.exists?("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt")
true

iex> COS.Object.exists?("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example/")
true

iex> COS.Object.exists?("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "missing.txt")
false
Link to this function

get(host, key, opts \\ [])

View Source
@spec get(
  host :: binary(),
  key :: binary(),
  opts :: [
    query: %{optional(:version_id) => binary()} | nil,
    headers: Tesla.Env.headers(),
    tesla_opts: Tesla.Env.opts()
  ]
) :: Tesla.Env.t()

下载对象 - 腾讯云文档

示例

示例

iex> COS.Object.put("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "content")
{:ok, ...}

iex> COS.Object.get("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "content")
{:ok, %Tesla.Env{
  body: "content",
  headers: [
    {"content-type", "application/octet-stream"},
    {"content-length", "7"},
    ...
  ],
  ...
}}

# 指定 Range 请求头部下载部分内容
iex> COS.Object.get("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "content", headers: [
       {"range", "bytes=0-2"}
     ])
{:ok, %Tesla.Env{
  body: "con",
  headers: [
    {"content-type", "application/octet-stream"},
    {"content-length", "3"},
    {"accept-ranges", "bytes"},
    {"content-range", "bytes 0-0/3"},
    ...
  ],
  ...
}}
Link to this function

get_presigned_url(host, key, opts \\ [])

View Source
@spec get_presigned_url(
  host :: binary(),
  key :: binary(),
  opts :: [
    method: Tesla.Env.method(),
    query: Tesla.Env.query(),
    headers: Tesla.Env.headers(),
    expired_at: DateTime.t(),
    expire_in: COS.Utils.expire_in()
  ]
) :: binary()

获取请求预签名 URL

说明:

  • 建议用户使用临时密钥生成预签名,通过临时授权的方式进一步提高预签名上传、下载等请求的安全性。 申请临时密钥时,请遵循最小权限指引原则,防止泄漏目标存储桶或对象之外的资源。
  • 如果您一定要使用永久密钥来生成预签名,建议永久密钥的权限范围仅限于上传或下载操作,以规避风险。
  • 获取对象的 URL 并下载对象参数,可在获取的 URL 后拼接参数 response-content-disposition=attachment

示例

示例

iex> COS.Object.get_presigned_url("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt")
"https://bucket-1250000000.cos.ap-beijing.myqcloud.com/example.txt?q-ak=AKIDb************************DmNIJ&q-header-list=&q-key-time=1649011703%3B1649012603&q-sign-algorithm=sha1&q-sign-time=1649011703%3B1649012603&q-signature=a9c13b2b1e09c5ce46df0242ac37b9cb828c0d6b&q-url-param-list="
Link to this function

get_to_file(host, key, path, opts \\ [])

View Source
@spec get_to_file(
  host :: binary(),
  key :: binary(),
  path :: binary(),
  opts :: [
    query: %{optional(:version_id) => binary()} | nil,
    headers: Tesla.Env.headers(),
    tesla_opts: Tesla.Env.opts()
  ]
) :: Tesla.Env.t()

下载对象到本地文件

示例

示例

iex> COS.Object.get_to_file("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "./example.txt")
{:ok, %Tesla.Env{
  body: "content",
  headers: [
    {"content-type", "application/octet-stream"},
    {"content-length", "7"},
    ...
  ],
  ...
}}
Link to this function

head(host, key, opts \\ [])

View Source
@spec head(
  host :: binary(),
  key :: binary(),
  opts :: [
    query: %{optional(:version_id) => binary()} | nil,
    headers: Tesla.Env.headers(),
    tesla_opts: Tesla.Env.opts()
  ]
) :: Tesla.Env.t()

查询对象元数据 - 腾讯云文档

Link to this function

multi_delete(host, body, opts \\ [])

View Source
@spec multi_delete(
  host :: binary(),
  body :: %{
    :object => [%{:key => binary(), optional(:version_id) => binary()}],
    optional(:quiet) => boolean()
  },
  opts :: [{:tesla_opts, Tesla.Env.opts()}]
) :: Tesla.Env.t()

删除多个对象 - 腾讯云文档

示例

示例

iex> COS.Object.multi_delete("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", %{
       object: [%{key: "example.txt"}, %{key: "error.txt"}]
     })
{:ok, %Tesla.Env{
  body: %{
    "deleted" => [%{
      "key" => "example.txt",
      ...
    }],
    "error" => [%{
      "key" => "error.txt",
      ...
    }]
  },
  ...
}}

# Quiet 模式,在响应中仅包含删除失败的对象信息和错误信息
iex> COS.Object.multi_delete("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", %{
       object: [%{key: "example.txt"}, %{key: "error.txt"}],
       quiet: true
     })
{:ok, %Tesla.Env{
  body: %{
    "deleted" => [],
    "error" => [%{
      "key" => "error.txt",
      ...
    }]
  },
  ...
}}
Link to this function

put(host, key, content, opts \\ [])

View Source
@spec put(
  host :: binary(),
  key :: binary(),
  content :: binary(),
  opts :: [headers: Tesla.Env.headers(), tesla_opts: Tesla.Env.opts()]
) :: Tesla.Env.t()

简单上传对象 - 腾讯云文档

可以上传一个对象至指定存储桶中,该操作需要请求者对存储桶有 WRITE 权限。 最大支持上传不超过5GB的对象,5GB以上对象请使用分块上传(Todo)或高级接口(Todo)上传。

示例

示例

iex> COS.Object.put("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "content")
{:ok, %Tesla.Env{
  body: "",
  headers: [
    {"server", "tencent-cos"},
    {"data", "Tue, 29 Mar 2022 16:39:58 GMT"},
    ...
  ],
  ...
}}

# 设置传对象的内容类型
COS.Object.put(
  "https://bucket-1250000000.cos.ap-beijing.myqcloud.com",
  "example.json",
  "{\"key\":\"value\"}",
  headers: [{"content-type", "application-json"}]
)

# 设置 HTTP 响应的超时时间
COS.Object.put(
  "https://bucket-1250000000.cos.ap-beijing.myqcloud.com",
  "example.txt",
  "content",
  tesla_opts: [adapter: [recv_timeout: 30_000]]
)

# 创建“文件夹”
COS.Object.put("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example/", "")
Link to this function

put_from_file(host, key, path, opts \\ [])

View Source
@spec put_from_file(
  host :: binary(),
  key :: binary(),
  path :: binary(),
  opts :: [headers: Tesla.Env.headers(), tesla_opts: Tesla.Env.opts()]
) :: Tesla.Env.t()

上传本地文件

示例

示例

iex> COS.Object.put_from_file("https://bucket-1250000000.cos.ap-beijing.myqcloud.com", "example.txt", "./example.txt")
{:ok, %Tesla.Env{
  body: "",
  headers: [
    {"server", "tencent-cos"},
    {"data", "Tue, 29 Mar 2022 16:39:58 GMT"},
    ...
  ],
  ...
}}
Link to this function

restore(host, key, body, opts \\ [])

View Source
@spec restore(
  host :: binary(),
  key :: binary(),
  body :: %{days: pos_integer(), c_a_s_job_parameters: %{tier: binary()}},
  opts :: [headers: Tesla.Env.headers(), tesla_opts: Tesla.Env.opts()]
) :: Tesla.Env.t()

恢复归档对象 - 腾讯云文档