如何使用cloudinary API下载文件?

How to download files using cloudinary API?

我试图制作一个程序,将 .txt 文件与 Cloudinary 同步。目的是使用 Heroku 运行 代码并在 dyno 重新启动时下载断言。问题是他们的网站上似乎没有下载功能(很可能是我不识字)。

我认为 fetch 函数可能是我要找的,但它似乎也用于上传。我也知道有一些函数(可能不是来自 Cloudinary)可以使用 link 下载。这个解决方案也很好,但我需要找到一种方法来获得 link (但我很确定它在你上传时得到 returned),但问题是,我的程序应该首先同步,这意味着我无法将 link 作为 return 值。我也想知道有没有办法删除文件(如果文件重名不自动替换)

我做了一个小样,可以上传文件,效果不错:

import cloudinary
import os
from cloudinary import uploader
from signal import signal, SIGINT, SIGTERM
import time

cloudinary.config(
  cloud_name = 'not_real',  
  api_key = '696969696996',  
  api_secret = 'helloWorld'  
)

def downloader():
  #something here...
  pass

downloader()

def handler(sig, frame):
  print(cloudinary.uploader.upload("TEST.txt", resource_type = "raw", public_id = "TEST"))

print("PID: ", os.getpid())
signal(SIGTERM, handler)   # to understand when the dyno is getting killed

while True:
  print("Doing nothing...")
  time.sleep(3600)

再一次(简而言之)我希望这样做:

  1. 通过 link 或名称下载文件。
  2. 删除旧文件(如果有必要)

如有任何帮助,我们将不胜感激。

upd:似乎转到 Cloudinaty .txt 文件的 link 打开了一个只有文本的空网页,所以我想我可以

r = requests.get("https://res.cloudinary.com/name/raw/upload/v1589215899/TEST.txt")
print(r.text)

,但不知道有没有更好的解决办法

raw 上传到 Cloudinary 后,会返回包含以下内容的响应:

{
  "public_id": "sample.txt",
  "version": 1371928603,
  "signature": "9088291a2c12202767cfa7c5e874afee72be78cd",
  "resource_type": "raw",
  "created_at": "2017-06-22T19:16:43Z",
  "tags": [],
  "bytes": 6144,
  "type": "upload",
  "etag": "107bf134b5afd11cc7544d60108d87b", 
  "url": "http://res.cloudinary.com/demo/raw/upload/v1371928603/sample.txt",
  "secure_url": 
         "https://res.cloudinary.com/demo/raw/upload/v1371928603/sample.txt"
  "original_filename": "myoriginaltextfile"
}

Cloudinary 建议存储 public_idresource_typetypeversion(可以省略)以便稍后重新生成 Cloudinary URL在。 public_id 是资产的唯一标识符。 resource_type 标识它是什么资源,例如imagevideorawtypedelivery type of that asset, e.g. upload (means public), private, and authenticated to name a few. version is the timestamp of when the asset was uploaded. With that being said, once you have those asset details, you can generate the Cloudinary URL by either doing a direct URL build or generate an image or video tag. The image/video tag wouldn't be helpful for raw files, so direct URL building would be the way to go. Cloudinary Python SDK (and any of their other SDKs) have similar functions, but the Python one can be found here.

cloudinary.utils.cloudinary_url("sample.txt", resource_type = "raw")
# Output: "https://res.cloudinary.com/demo/raw/upload/sample.txt"