| 承载协议 | HTTP PATCH |
|---|---|
| 接口方向 | 开发者服务器->华为云空间服务器 |
| 接口URL | https://driveapis.cloud.huawei.com.cn/upload/drive/v1/files/{fileId}?uploadType=multipart |
| 数据格式 | 请求消息:Content-Type: multipart/related 响应消息:Content-Type: application/json |
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| uploadType | 是 | string | 更新方式。 |
| fields | 否 | string | 采用[Partial-response格式],具体使用见公共说明。 |
| addParentFolder | 否 | string | 逗号分隔的父目录ID列表,注意只取第一个,不支持多父目录,addParentFolder必须和removeParentFolder成对出现。 |
| removeParentFolder | 否 | string | 逗号分隔的父目录ID列表,注意只取第一个,不支持多父目录,addParentFolder必须和removeParentFolder成对出现。 |
| form | 否 | string | 媒体格式。 |
| prettyPrint | 否 | boolean | 是否以美观格式输出。 |
| quotaId | 否 | string | 用户标识,小于40个字符。用于限制单个用户的API访问数量。 |
| callback | 否 | string | JSONP的callback函数。 |
| autoRename | 否 | string | 自动重命名,如果是true,或者不传,自动重命名;如果是false,则不会自动重命名。 |
Request Header
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| Authorization | 是 | string | 用户鉴权信息 AccessToken(即AT) |
| x-hw-trace-id | 否 | string | 业务跟踪id。 |
| x-hw-app-id | 否 | string | 应用的appid。 |
| version | 否 | string | SDK版本。 |
| x-hw-terminal | 否 | string | 终端型号。 |
| x-hw-os | 否 | string | 终端操作系统类型。 |
| unionID | 否 | string | 用户的unionID。 |
| User-Agent | 否 | string | 应用版本号/appid。 |
| x-hw-deviceUUID | 否 | string | 设备UUID。 |
| x-hw-deviceUDID | 否 | string | 设备UDID。 |
| x-hw-appPackageName | 否 | string | 应用包名。 |
| x-hw-network | 否 | string | 网络类型,[WiFi, 2G, 3G, 4G, 5G, wire]。 |
| Cookie | 否 | string | cookie中的loginId为会话ID,用于鉴权。 |
Request Body
| 参数 | 参数类型 | 描述 |
|---|---|---|
| appSettings | string | 应用自定义属性。 |
| contentExtras.thumbnail.content | string | 缩略图,base 64编码。 |
| contentExtras.thumbnail.mimeType | string | 缩略图格式。 |
| writerHasCopyPermission | boolean | copy是否需要writer权限。 |
| createdTime | string | 文件创建时间。 |
| description | string | 文件描述。 |
| mimeType | string | 文件类型,具体取值参照公共说明。 |
| editedTime | string | 文件修改时间。 |
| fileName | string | 文件名。 |
| originalFilename | string | 原始文件名。 |
| parentFolder[] | string | 父目录,可填[root,applicationData,普通目录的id]。 |
| properties | string | 文件公共自定义属性。 |
| favorite | boolean | 文件是否被收藏。 |
| writersHasSharePermission | boolean | 写权限是否能分享。 |
| sha256 | string | 文件的sha256。 |
加文件实体
PATCH https://driveapis.cloud.huawei.com.cn/upload/drive/v1/files/AB9FPewYCuoCKgA0li1P43gSADKWIArsA?uploadType=multipart&fields=* HTTP/1.1
Content-Type: multipart/related;boundary=OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO
Authorization: Bearer CV3PnPLZXyr9K2GV7RYZkF962JZvoEkMQhTho3QHQAPQUyma/lEyZskP/IMWrc7FvaeHivO7y0lyibI/fQvulBnTgtXArSVPsjaLKvYNkDsJSaIR2/sx+K40bYDWWpKFvJNKg2U47jRwz2mfiF3ortNk
Cache-Control: no-cache
--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO
Content-Type: application/json
// 换行必填
{
"fileName":"zztest0014",
"contentExtras": {
"thumbnail":{
"content":"dGVzdDAwMw==",
"mimeType":"image/jpeg"
}
},
"parentFolder":["applicationData"]
}
--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO
Content-Type: application/octet-stream
//换行必填
[imageData]
--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO-- 状态码为200时:
| 参数 | 参数类型 | 描述 |
|---|---|---|
| category | string | 类型,固定为drive#file。 |
| id | string | 文件id。 |
| fileName | string | 文件名。 |
| mimeType | string | 文件类型,具体取值参照公共说明。 |
| description | string | 文件描述。 |
| favorite | boolean | 文件是否被收藏。 |
| recycled | boolean | 文件是否被放入回收站。 |
| directlyRecycled | boolean | 文件是否被直接被放入回收站。 |
| recyclingUser.category | string | 类型,固定为drive#user。 |
| recyclingUser.displayName | string | 用户显示名。 |
| recyclingUser.profilePhotoLink | string | 用户头像链接(默认不返回)。 |
| recyclingUser.me | boolean | 是否是我。 |
| recyclingUser.permissionId | string | 权限id。 |
| recyclingUser.userAccount | string | 帐号名称(默认不返回)。 |
| recycledTime | string | 放入回收站的时间。 |
| parentFolder[] | string | 父目录。 |
| properties | string | 文件公共自定义属性。 |
| appSettings | string | 应用自定义属性。 |
| containers[] | string | 空间: drive,applicationData。 |
| version | integer | 版本号。 |
| contentDownloadLink | string | 文档下载链接。 |
| iconDownloadLink | string | 图标链接。 |
| existThumbnail | boolean | 是否有缩略图。 |
| thumbnailDownloadLink | string | 缩略图链接。 |
| smallThumbnailDownloadLink | string | 小缩略图链接。 |
| thumbnailVersion | integer | 缩略图版本号。 |
| onLineViewLink | string | 在线浏览链接。 |
| createdTime | string | 文件创建时间。 |
| editedTime | string | 文件修改时间。 |
| editedByMeTime | string | 我修改文件的时间。 |
| editedByMe | boolean | 是否是我修改文件。 |
| sharedWithMeTime | string | 共享给我的时间。 |
| sharer.category | string | 类型,固定为drive#user。 |
| sharer.displayName | string | 用户显示名。 |
| sharer.profilePhotoLink | string | 用户头像链接(默认不返回)。 |
| sharer.me | boolean | 是否是我。 |
| sharer.permissionId | string | 权限id。 |
| sharer.userAccount | string | 帐号名称(默认不返回)。 |
| owners.category | string | 类型,固定为drive#user。 |
| owners.displayName | string | 用户显示名。 |
| owners.profilePhotoLink | string | 用户头像链接(默认不返回)。 |
| owners.me | boolean | 是否是我。 |
| owners.permissionId | string | 权限id。 |
| owners.userAccount | string | 帐号名称(默认不返回)。 |
| lastEditor.category | string | 类型,固定为drive#user。 |
| lastEditor.displayName | string | 用户显示名。 |
| lastEditor.profilePhotoLink | string | 用户头像链接(默认不返回)。 |
| lastEditor.me | boolean | 是否是我。 |
| lastEditor.permissionId | string | 权限id。 |
| lastEditor.userAccount | string | 帐号名称(默认不返回)。 |
| hasShared | boolean | 是否共享。 |
| ownedByMe | boolean | 是否是我用。 |
| capabilities.addChildNodePermission | boolean | 是否可以增加子目录/文件。 |
| capabilities.writerHasChangeCopyPermission | boolean | change copy是否需要write权限。 |
| capabilities.commentPermission | boolean | 是否可以评论。 |
| capabilities.copyPermission | boolean | 是否可以拷贝。 |
| capabilities.deletePermission | boolean | 是否可以删除。 |
| capabilities.downloadPermission | boolean | 是否可以下载内容。 |
| capabilities.editPermission | boolean | 是否可以编辑。 |
| capabilities.listChildNodePermission | boolean | 是否可以list子目录/文件。 |
| capabilities.editContentPermission | boolean | 是否可以修改内容。 |
| capabilities.readHistoryVersionPermission | boolean | 是否可以读取副本。 |
| capabilities.removeChildNodePermission | boolean | 是否可以移动子目录。 |
| capabilities.renameFilePermission | boolean | 是否可以重命名。 |
| capabilities.shareFilePermission | boolean | 是否可以共享。 |
| capabilities.recyclePermission | boolean | 是否可以移动到回收站。 |
| capabilities.unrecyclePermission | boolean | 是否可以撤回移动子文件到回收站。 |
| viewersHasCopyContentPermission | boolean | 浏览者是否可以拷贝内容。 |
| writerHasCopyPermission | boolean | 拷贝是否需要writer权限。 |
| writersHasSharePermission | boolean | writer能否share。 |
| permissions[] | list | permission列表。 |
| permission.category | string | 类型,固定为drive#permission。 |
| permission.id | string | 权限ID。 |
| permission.type | string | 类型,[user,anyone]。 |
| permission.role | string | 角色,[owner,writer,commenter,reader]。 |
| permission.userAccount | string | 帐号名称。 |
| permission.displayName | string | 名称。 |
| permission.expirationTime | string | 失效时间。 |
| permission.deleted | boolean | 权限是否已删除。 |
| permission.canDiscoverFile | boolean | 是否允许被搜索(仅适用于anyone类型)。 |
| permissionIds | list | permissionId列表。 |
| originalFilename | string | 原始文件名。 |
| fullFileSuffix | string | 完整的文件后缀,取name第一个.后的字符串。 |
| fileSuffix | string | 文件后缀,fullFileExtension最后一个字符串。 |
| size | integer | 文件大小。 |
| occupiedSpace | integer | 实际占用空间大小。 |
| pictureMetadata.width | integer | 照片宽度。 |
| pictureMetadata.height | integer | 照片高度。 |
| pictureMetadata.location.latitude | number | 纬度。 |
| pictureMetadata.location.longitude | number | 经度。 |
| pictureMetadata.location.altitude | number | 高度。 |
| pictureMetadata.exifTime | string | 照片的时间,EXIF DateTime。 |
| pictureMetadata.cameraMaker | string | 照相机制造商。 |
| pictureMetadata.cameraModel | string | 照相机型号。 |
| pictureMetadata.exposureTime | number | 曝光长度,单位秒。 |
| pictureMetadata.aperture | number | 光圈,f-number。 |
| pictureMetadata.flashUsed | boolean | 是否使用闪光灯。 |
| pictureMetadata.focalLength | number | 焦距,毫米。 |
| pictureMetadata.isoSpeed | integer | ISO速度。 |
| pictureMetadata.meteringMode | string | 测距模式。 |
| pictureMetadata.sensor | string | 感光类型。 |
| pictureMetadata.exposureMode | string | 曝光模式。 |
| pictureMetadata.colorSpace | string | 色彩空间。 |
| pictureMetadata.whiteBalance | string | 白平衡模式。 |
| pictureMetadata.exposureBias | number | 曝光偏差,APEX值。 |
| pictureMetadata.maxApertureValue | number | 最大光圈值,APEX value。 |
| pictureMetadata.lens | string | 镜头。 |
| videoMetadata.width | integer | 视频宽度。 |
| videoMetadata.height | integer | 视频高度。 |
| videoMetadata.durationTime | integer | 时长,毫秒。 |
| sha256 | string | 文件的sha256 |
| lastHistoryVersionId | string | 最新的历史记录版本ID。 |
| contentVersion | string | 内容版本号。 |
{
"fileName":"hwe003",
"sha256":"59f29d7b3a84482f3ae641d1d490210c589440c6e5104e9ab9574c4aeb40550e",
"owners":[
{
"permissionId":"239046878371512704",
"displayName":"*******3121@**3.com",
"me":true,
"category":"drive#user"
}
],
"hasShared":false,
"mimeType":"application/octet-stream",
"permissionIds":[
"239046878371512704"
],
"lastHistoryVersionId":"ABgL5WYKCuoCLU7sHD9P43gVTuocMgrsA",
"parentFolder":[
"AgFovWXIAIYANC20H_1Fj3oMLbIf8ACAA"
],
"recycled":false,
"directlyRecycled":false,
"permissions":[
{
"role":"owner",
"deleted":false,
"displayName":"*******3121@**3.com",
"id":"239046878371512704",
"category":"drive#permission",
"type":"user",
"allowFileDiscovery":true
}
],
"occupiedSpace":11198,
"createdTime":"2020-06-13T06:52:19.000Z",
"id":"ABgL5WYKCuoCLU7sHD9P43gVTuocMgrsA",
"contentDownloadLink":"https://driveapis.cloud.huawei.com.cn/drive/v1/files/ABgL5WYKCuoCLU7sHD9P43gVTuocMgrsA?form=content",
"capabilities":{
"downloadPermission":true,
"renameFilePermission":true,
"commentPermission":true,
"deletePermission":true,
"readHistoryVersionPermission":true,
"recyclePermission":true,
"addChildNodePermission":false,
"unrecyclePermission":true,
"writerHasChangeCopyPermission":false,
"copyPermission":true,
"editContentPermission":true,
"removeChildNodePermission":false,
"editPermission":true,
"listChildNodePermission":false,
"shareFilePermission":true
},
"ownedByMe":true,
"version":1592031003000,
"writersHasSharePermission":true,
"iconDownloadLink":"https://event-dre.dbankcdn.com/filemanagerpic/201911140913476Sc7.png",
"editedTime":"2020-06-13T06:52:19.000Z",
"size":11198,
"thumbnailVersion":0,
"lastEditor":{
"permissionId":"239046878371512704",
"displayName":"*******3121@**3.com",
"me":true,
"category":"drive#user"
},
"existThumbnail":true,
"containers":[
"drive"
],
"category":"drive#file",
"contentVersion":"4ed95d13-d9e4-334f-aab7-ef49c21d78b6"
} 错误码后4位为业务错误码,用于区分错误场景,其他业务错误码见状态码章节。
| 状态码 | 错误码 | 响应代码 | 描述 |
|---|---|---|---|
| 200 | NA | - | 成功。 |
| 400 | 22024001 | LACK_OF_PARAM | 缺少入参,请检查参数列表。 |
| 400 | 22024002 | PARAM_INVALID | 参数校验失败,请检查参数列表。 |
| 400 | 22234002 | PARAM_INVALID | 参数校验失败,请检查参数列表 |
| 400 | 22024003 | PARAM_VALUE_NOT_SUPPORT | 无效参数,请检查参数列表。 |
| 400 | 22024004 | PARAM_EXPIRED | 参数不支持,请检查参数列表。 |
| 400 | 22054005 | SITE_NOT_FOUND | 站点信息未知。 |
| 401 | 22044011 | USER_NOT_AUTHORIZED | 用户鉴权失败。 |
| 403 | 22124031 | DATA_MIGRATING | 正处于安全策略更新, 请等待安全策略更新完成。 |
| 403 | 22004032 | SERVICE_NOT_SUPPORT | 服务在当前国家/地区不支持。 |
| 403 | 22044033 | AGREEMENT_NOT_SIGNED | 协议未签署,请签署协议后再进行重试。 |
| 403 | 22054034 | CROSS_SITE_NOT_SUPPORT | 跨站点功能限制。 |
| 403 | 22004035 | INSUFFICIENT_SCOPE | Scope校验失败。 |
| 403 | 22234035 | INSUFFICIENT_SCOPE | Scope校验失败。 |
| 403 | 22234036 | INSUFFICIENT_PERMISSION | 无此操作权限。 |
| 403 | 22024038 | OUTER_SERVICE_ERROR | 外部服务错误且无法恢复。 |
| 403 | 22234038 | OUTER_SERVICE_ERROR | 外部服务错误且无法恢复。 |
| 403 | 22114039 | SPACE_FULL | 用户空间不足,请扩容或删除文件后再进行重试。 |
| 404 | 22234041 | CONTENT_NOT_FOUND | 内容未发现。 |
| 429 | 22074291 | USER_REQUEST_TOO_MANY | 用户请求太频繁。 |
| 429 | 22074292 | APP_REQUEST_TOO_MANY | 应用请求太频繁。 |
| 500 | 22025002 | OUTER_SERVICE_UNAVAILABLE | 外部服务不可用。 |
| 500 | 22235002 | OUTER_SERVICE_UNAVAILABLE | 外部服务不可用。 |
| 500 | 22005006 | SERVER_TEMP_ERROR | 服务器临时错误。 |
public static void main(String[] args) throws Exception {
// 设置请求地址和用户认证令牌
String url = "https://driveapis.cloud.huawei.com.cn/upload/drive/v1/files";
String fileId = "Bkw3QjQolVUoYQdEk13lFX5BekY2GJVTK";
String access_token = "DAEAANApuUTBWbVKLu7HkNytlhHg+naBsKszPZ6V/uY7wIaHReDtI3sgUNmcJ1D/wcgkW6yGl5ht6fxcW3+olmgMgkS/ypJxkw89icLwzxXBbX2h3ochRK8=";
// 修改并上传文件(multipart方式)
JSONObject fileInfo = filesUpdatePatchMultipart(url, access_token, fileId);
System.out.println(fileInfo.toJSONString());
}
private static JSONObject filesUpdatePatchMultipart(String url, String access_token, String fileId)
throws Exception {
StringBuilder stringBuilder = new StringBuilder("");
stringBuilder.append("/").append(fileId);
stringBuilder.append("?").append("fields=*").append("&").append("uploadType=multipart");
HttpPatch httpPatch = new HttpPatch(url + stringBuilder);
httpPatch.setHeader("Authorization", "Bearer " + access_token);
httpPatch.setHeader("Content-Type", "multipart/related;boundary=OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO");
httpPatch.setHeader("Accept", "application/json");
FileInputStream fis = new FileInputStream("D:\\5555555.PNG");// The path of your local file
ByteArrayOutputStream fos = new ByteArrayOutputStream();
byte[] tempData = new byte[1024];
int count = 0;
// 第一部分 元数据
byte[] multipartContentPart1 = ("--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO" + System.lineSeparator()
+ "Content-Type: application/json" + System.lineSeparator() + System.lineSeparator() + "{"
+ System.lineSeparator() + "\"fileName\":\"hw001.jpg\"," + System.lineSeparator() + "\"contentExtras\": {"
+ System.lineSeparator() + "\"thumbnail\":{\"content\":\"dGVzdDAwMw==\",\"mimeType\":\"image/png\"}},"
+ System.lineSeparator() + "\"parents\":[\"applicationData\"]" + System.lineSeparator() + "}"
+ System.lineSeparator() + "--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO" + System.lineSeparator()
+ "Content-Type: application/octet-stream" + System.lineSeparator() + System.lineSeparator()).getBytes();
fos.write(multipartContentPart1);
// 第二部分文件内容
while ((count = fis.read(tempData)) != -1) {
fos.write(tempData, 0, count);
}
// 第三部分文件尾分隔符
byte[] multipartContentPart3 = (System.lineSeparator() + "--OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO--").getBytes();
fos.write(multipartContentPart3);
ByteArrayEntity entity = new ByteArrayEntity(fos.toByteArray());
entity.setContentType("multipart/related;boundary=OP8XTaXZ0UZs-Sjlefcj2OWskqXWwVQO");
httpPatch.setEntity(entity);
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpPatch);
try {
HttpEntity responseEntity = response.getEntity();
String ret = responseEntity != null ? EntityUtils.toString(responseEntity) : null;
JSONObject jsonObject = (JSONObject) JSON.parse(ret);
EntityUtils.consume(responseEntity);
return jsonObject;
} finally {
response.close();
}
}