上传文件到七牛后返回hash/key/persistentId值, 如何获取文件的url访问地址?

在javascript(或其他服务端语言), 往7牛官方上传地址(http://upload.qiniu.com/), 提交上传后, 返回值大概如下:

{"hash":"Fn1Fxzactt76AaJdn9F4XQAkBkmp","key":"e12c391016.mp4","persistentId":"z0.5b5ed2c838b9f324a52d1e31","avinfo_duration":"7.170578"}

那怎么通过这返回值的值: hash/key/persistentId 取得上传的真正链接地址呢?

其实很简单, web访问地址就是: 空间域名+文件名(key值)

拿上面的例子,假如我的7牛空间域名是:https://video.xxxxx.com

最终的链接为:https://video.xxxxx.com/e12c391016.mp4

就是这么简单, 不需要通过hash/id 请求接口去取真实地址这么麻烦.

注意: key值是可以自定义的, 直接上代码(angular版本的上传), 注意key是自己提供的参数, 所以你需要自己处理唯一性. 例子直接写死了

for (let i = 0; i < files.length; i++) {
            const formData = new FormData();
            formData.append('file', files[i]);
            formData.append('key', "12c391016.mp4");//自定义key值
            formData.append('token', this.uptoken);
            this.loading = true;
            const request = new HttpRequest(
                'POST', this.upHost , formData,
                {reportProgress: true});
            this.http.request(request)
                .retry(3)
                .subscribe(
                    event => {
                        if (event.type === HttpEventType.UploadProgress) {
                            this.loading = true;
                            this.percentDone = Math.round(100 * event.loaded / event.total);
                            this.progress = `File is ${this.percentDone}% uploaded.`;
                        } else if (event instanceof HttpResponse) {
                            this.progress = `${event.body['key']} is uploaded`;
        }});
            this.loading = false;
        }

参考:https://segmentfault.com/q/1010000007459599

猜你喜欢

转载自blog.csdn.net/cen_cs/article/details/81288821