【问题标题】:Uploading a file to Figshare using rapiclient swagger api使用 rapiclient swagger api 将文件上传到 Figshare
【发布时间】:2021-06-12 06:11:39
【问题描述】:

我正在尝试使用 rapiclient 以编程方式更新我的 Figshare 存储库。 在answer 到question 之后,我设法通过以下方式进行身份验证并查看我的存储库:

library(rapiclient)
library(httr)

# figshare repo id
id = 3761562

fs_api <- get_api("https://docs.figshare.com/swagger.json")
header <- c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT")))
fs_api <- list(operations = get_operations(fs_api, header), 
               schemas = get_schemas(fs_api))
reply <- fs_api$operations$article_files(id)

我还设法使用以下方法删除了一个文件:

fs_api$operations$private_article_file_delete(article_id = id, file_id = F) 

现在,我想将一个新文件上传到存储库。我似乎需要两种方法:

fs_api$operations$private_article_upload_initiate
fs_api$operations$private_article_upload_complete

但我不理解文档。根据fs_api$operations$private_article_upload_initiate帮助:

> fs_api$operations$private_article_upload_initiate
private_article_upload_initiate
Initiate Upload
Description:
  Initiate new file upload within the article. Either use link to
  provide only an existing file that will not be uploaded on figshare
  or use the other 3 parameters(md5, name, size)

Parameters:
  link (string)
    Url for an existing file that will not be uploaded on figshare
  md5 (string)
    MD5 sum pre computed on the client side
  name (string)
    File name including the extension; can be omitted only for linked
    files.
  size (integer)
    File size in bytes; can be omitted only for linked files.

“不会上传到 Figshare 的文件”是什么意思?如何使用 API 上传本地文件~/foo.txt?

fs_api$operations$private_article_upload_initiate(link='~/foo.txt') 

返回 HTTP 400。

【问题讨论】:

    标签: r openapi rfigshare


    【解决方案1】:

    我觉得我用我的previous answer 让你走上了一条坏路,因为我不确定在使用rapiclient 时如何编辑一些 api 端点。例如,fs_api$operations$private_article_upload_initiate() 对应的端点是https://api.figshare.com/v2/account/articles/{article_id}/files,我不知道在发送请求之前如何替换{article_id}。

    您可能必须为无法以任何其他方式工作的操作定义自己的客户端。

    这是根据您的问题目标将文件上传到现有私人文章的示例。

    library(httr)
    
    # id of previously created figshare article
    my_article_id <- 99999999
    
    # make example file to upload
    my_file <- tempfile("my_file", fileext = ".txt")
    writeLines("Hello World!", my_file)
    
    # Step 1 initiate upload
    # https://docs.figshare.com/#private_article_upload_initiate
    r <- POST(
      url = sprintf("https://api.figshare.com/v2/account/articles/%s/files", my_article_id),
      add_headers(c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT")))),
      body = list(
        md5  = tools::md5sum(my_file)[[1]], 
        name = basename(my_file), 
        size = file.size(my_file)
      ),
      encode = "json"
    )
    initiate_upload_response <- content(r)
    
    # Step 2 single file info (get upload url)
    # https://docs.figshare.com/#private_article_file
    r <- GET(url = initiate_upload_response$location,
             add_headers(c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT"))))
    )
    single_file_response <- content(r)
    
    # Step 3 uploader service (get number of upload parts required)
    # https://docs.figshare.com/#endpoints
    r <- GET(url = single_file_response$upload_url, 
             add_headers(c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT"))))
    )
    upload_service_response <- content(r)
    
    # Step 4 upload parts (this example only has one part)
    # https://docs.figshare.com/#endpoints_1
    r <- PUT(url = single_file_response$upload_url, path = 1, 
             add_headers(c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT")))),
             body = upload_file(my_file)
    )
    upload_parts_response <- content(r)
    
    # Step 5 complete upload (after all part uploads are successful)
    # https://docs.figshare.com/#private_article_upload_complete
    r <- POST(
      url = initiate_upload_response$location,
      add_headers(c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT"))))
    )
    complete_upload_response <- content(r)
    

    【讨论】:

    • 哇,非常感谢!在接受之前我需要对此进行一些测试,但现在谢谢你
    • 到目前为止,您的帮助非常大。不幸的是,我遇到了一个新问题。在第 4 步中,我收到错误 Cannot PUT /1。您对如何开始调试有什么想法吗?
    • 上传服务是说要上传文件的一部分(如我的例子)还是多部分?
    • 您的确切示例(我将自己的 article_id 替换为 99999999)给出了此错误。我还没有机会弄清楚分段上传。
    • 当然这个例子对我有用,但我想不出任何可能对你来说出错的地方。也许,另一个问题可能会让其他人想到一些事情。
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2022-08-17
    • 2020-09-05
    • 2016-07-21
    • 2011-09-25
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多