网盘(附件)

网盘(附件)相关接口,仅 Chalk 3 支持

文件上传使用方式

使用步骤

  1. 使用「创建文件 Meta 信息」接口创建文件 Meta 信息,获取文件 ID ;
    1. 其中的 hash 由客户端自行计算,使用 md5 对文件内容进行哈希取值;
  2. 使用「获取指定文件的上传 policy」获取文件上传策略;
  3. 使用文件上传策略返回数据上传文件;

使用示例 (PHP)

附件上传

use GuzzleHttp\Client;

$token = $token['access_token']; // 从 authorize 接口获取

$client = new Client();

// 1. 创建文件 meta 信息

$file = '/path/to/file.json';
$file = json_decode($client->post('http://open.develop.ci.seiue.com/api/v3/netdisk/files', [
    'headers' => [
        'Authorization' => 'Bearer ' . $token,
        'X-School-Id' => 589,
        'Content-Type' => 'application/json'
    ],
    'json' => [
        'netdisk_owner_id' => 0, // 附件固定传 0
        'name' => basename($file),
        'size' => filesize($file),
        'hash' => md5_file($file),
        'mime' => 'application/json',
    ]
])->getBody(), true);

var_dump($file);


// 2. 获取文件上传策略信息

$filePolicy = json_decode($client->get('http://open.develop.ci.seiue.com/api/v3/netdisk/files/' . $file['id'] . '/policy',
    [
        'headers' => [
            'Authorization' => 'Bearer ' . $token,
            'X-School-Id' => 589
        ]
    ])->getBody(), true);

var_dump($filePolicy);

// 3. 上传文件
// form/multipart 方式上传文件,上传字段参见示例,不可新增或缺少字段

try {
    $content = file_get_contents('/Users/devon/Desktop/logo.jpg');
    $upload = $client->post($filePolicy['host'], [
        'multipart' => [
            [
                'name' => 'key',
                'contents' => $filePolicy['object_key']
            ],
            [
                'name' => 'OSSAccessKeyId',
                'contents' => $filePolicy['access_key_id']
            ],
            [
                'name' => 'host',
                'contents' => $filePolicy['host']
            ],
            [
                'name' => 'policy',
                'contents' => $filePolicy['policy']
            ],
            [
                'name' => 'signature',
                'contents' => $filePolicy['signature']
            ],
            [
                'name' => 'callback',
                'contents' => $filePolicy['callback']
            ],
            [
                'name' => 'expire',
                'contents' => $filePolicy['expire']
            ],
            [
                'name' => 'file',
                'contents' => $content,
            ]
        ],
    ])->withoutHeader('Content-Length'); // GuzzleHttp 客户端会自动添加 Content-Length,但 OSS 不支持 Content-Length
} catch (\Throwable $e) {
    var_dump((string)$e->getResponse()->getBody());
}

var_dump($upload->getBody()->getContents());

// 至此已上传完成
// 在各可上传附件的接口中可使用,一般字段名均为 `attachments[*].hash=`

网盘文件上传

网盘信息接口暂未开放,暂不支持。

results matching ""

    No results matching ""