REST设计用于文件上传 [英] REST design for file uploads

查看:146
本文介绍了REST设计用于文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为文件上传服务创建REST API,从而允许用户执行以下操作:

I want to create a REST API for a file upload service that allows a user to:

  1. 打开会话
  2. 上传一堆文件
  3. 关闭会话

然后再回来,对它们在上一个会话中上传的文件进行处理.

And then later, come back and do things with the files they uploaded in a previous session.

为了便于处理有关每个文件的数据和处理文件本身的内容,这是我正在考虑使用的URI方案:

To facilitate dealing with data about each file and dealing with the content of the file itself, this is the URI scheme I am thinking of using:

/sessions/
/sessions/3
/sessions/3/files
/sessions/3/files/5
/sessions/3/file/5/content
/sessions/3/file/5/metadata

这将使文件元数据与文件内容分开处理.在这种情况下,文件 content 和文件 metadata 上只允许GET,并且要更新这两个文件,必须将一个新文件放入PUT.

This will allow the file metadata to be dealt with separately from the file content. In this case, only a GET is allowed on the file content and file metadata, and to update either one, a new file has to be PUT.

这有意义吗?如果没有,为什么以及如何会更好呢?

Does this make sense? If not, why and how could it be better?

推荐答案

为什么需要会话?是出于身份验证和授权的原因吗?如果是这样,我可以将HTTP 基本与SSL或

Why do you need sessions? Is it for Authentication and Authorization reasons? If so I would use http basic with SSL or digest. As such there is no start or end session, because http is stateless and security headers are sent on each request.

建议上传资源是直接映射为私有文件系统

Suggestion of upload resource would be to directly map as private filesystem



# returns all files and subdirs of root dir
GET /{userId}/files
GET /{userId}/files/file1
GET /{userId}/files/dir1
# create or update file
PUT /{userId}/files/file2



上传文件内容时,您将使用多部分内容类型.

When uploading file content you then would use multipart content type.

我会通过在上传负载中引入(指向文件内容的)链接来设计您想要的文件内容和负载分离.简化了资源结构.

I would design your wanted separation of file-content and payload by introducing link (to file-content) inside upload payload. It eases resource structure.

表示上传"资源:



{
  "upload-content" : "http://storage.org/2a34cafa" ,
  "metadata" : "{ .... }" 
}

资源操作:



# upload file resource
POST /files
-> HTTP 201 CREATED 
-> target location is shown by HTTP header 'Location: /files/2a34cafa

# /uploads as naming feels a bit more natural as /files
POST /sessions/{sessionId}/uploads
-> HTTP 201 CREATED
-> HTTP header: 'Location: /sessions/{sessionId}/uploads/1
-> also returning payload

# Updating upload (like metadata)
/PUT/sessions/{sessionId}/uploads/1 


这篇关于REST设计用于文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆