bishuihanshan
bishuihanshan
  • 发布:2017-09-19 12:19
  • 更新:2020-01-06 23:43
  • 阅读:5610

阿里云的OSS怎么上传

分类:HTML5+

用它那个multipartUpload()我总是不成功。
总是遇到下面这个问题:
Error: _getFileSize requires Buffer/File/String.
不知道multipartUpload()的file参数应该用什么?

2017-09-19 12:19 负责人:无 分享
已邀请:
回梦無痕

回梦無痕 - 暂停服务

oss的web的multipartUpload上传中,file参数是File对象,就是能从这个File对象可以获取name、size、lastModifiedDate和type等属性的

  • bishuihanshan (作者)

    谢谢你的回答,我是用了下面这样的做法,不知道这种方式得到的file是不是不符合oss的那个file,导致出现Error: _getFileSize requires Buffer/File/String.


    var relativePath = "_downloads/orig.jpg";

    plus.io.resolveLocalFileSystemURL(relativePath, function(entry){

    entry.file(function(file){

    var storeAs = 'abcdef';

    //console.log(file.name + ' => ' + storeAs);

    //console.log(file + ' = ' + JSON.stringify(file));

    accessKeyId: 'xxxx',

    accessKeySecret: 'yyyy',

    endpoint: 'oss-cn-shenzhen.aliyuncs.com',

    bucket: 'resp1'

    });


    client.multipartUpload(storeAs, file).then(function (result) {  
    console.log("result: " + JSON.stringify(result));
    }).catch(function (err) {
    console.log("error: " + err);
    });

    },function(e){

    //mui.toast("读写出现异常: " + e.message );

    console.log("读写出现异常: " + e.message);

    });


    2017-09-19 14:34

  • bishuihanshan (作者)

    我看了我获取的file对象,这是有size、type、name、lastModifiedDate、fullPath属性的。但是提示的问题是_getFileSize requires Buffer/File/String,是说这个file不支持_getFileSize吗


    2017-09-19 15:04

  • 回梦無痕

    回复 bishuihanshan:你贴上来的代码是不是不全?中间好像缺了一段


    2017-09-19 15:29

  • bishuihanshan (作者)

    没缺吧?应该够了吧


    2017-09-19 15:36

回梦無痕

回梦無痕 - 暂停服务

如果你没有删除某一些行的代码,那代码严重错误,比如

entry.file(function(file){  
var storeAs = 'abcdef';  
//console.log(file.name + ' => ' + storeAs);  
//console.log(file + ' = ' + JSON.stringify(file));  
accessKeyId: 'xxxx',  
accessKeySecret: 'yyyy',  
endpoint: 'oss-cn-shenzhen.aliyuncs.com',  
bucket: 'resp1'  
});

accessKeyId: 'xxxx',开始是错误的,这是结构体对象的赋值,不应该出现在这里
并且accessKeyId是新建client的参数,你检查一下代码。

回梦無痕

回梦無痕 - 暂停服务

以下实例,是按照OSS文档写的,没有经过测试,可能有报错,但大体就是这个样子的。

var relativePath = "_downloads/orig.jpg";  
plus.io.resolveLocalFileSystemURL(relativePath,  
	function(entry){//success  
		entry.file(function(file){  
			OSS.urllib.request("http://your_sts_server/",{method: 'GET'},   
				function (err, response) {  
			    		if(err){  
			        		return alert(err);  
			    		}  
			    		try {  
			        		result = JSON.parse(response);  
			        	}   
			        	catch (e) {  
			        		return alert('parse sts response info error: ' + e.message);  
			        	}  
			        	var client = new OSS.Wrapper({  
				        	accessKeyId: result.AccessKeyId,  
				        	accessKeySecret: result.AccessKeySecret,  
				        	stsToken: result.SecurityToken,  
				        	endpoint: '<oss endpoint>',  
				        	bucket: '<Your bucket name>'  
			        	});  
			    		client.multipartUpload(storeAs, file)  
				    	.then(function (result) {  
				        	console.log(result);  
				    	})  
				    	.catch(function (err) {  
				    		console.log(err);  
				    	});  
		        	}  
        		);  
			  
		});  
	},  
	function(e){//err  
		console.log("读写出现异常: " + e.message);  
	}  
);
  • bishuihanshan (作者)

    你这个跟我原来的写法差不多,就是多了获取key的过程,我运行你这个的结果跟我原来的结果一样,都是提示_getFileSize问题。可能是这个file不是参数要求的file?


    2017-09-19 16:09

  • 回梦無痕

    回复 bishuihanshan:multipartUpload要放在entry.file里运行。


    2017-09-19 16:10

  • bishuihanshan (作者)

    是在entry.file()里面运行的。file是有数据的,能够打出来它的size等属性


    2017-09-19 16:16

  • 回梦無痕

    回复 bishuihanshan:我看你贴出来的代码,multipartUpload就不是在entry.file()里面运行的


    2017-09-19 16:20

  • 回梦無痕

    回复 bishuihanshan:你重新贴一份完整的代码出来吧。


    2017-09-19 16:22

  • bishuihanshan (作者)

    回复 回梦無痕:重新贴了,在最后面。怎么贴出黑底的这种格式啊?


    2017-09-19 16:57

bishuihanshan

bishuihanshan (作者)

哦,是的,accessKeyId: 'xxxx'之前还有下面这行,我不小心搞掉了。
但是这个不影响那个file的理解吧。

var client = new OSS.Wrapper({

bishuihanshan

bishuihanshan (作者)

var relativePath = "_downloads/orig.jpg";
plus.io.resolveLocalFileSystemURL(relativePath, function(entry){
entry.file(function(file){
var storeAs = 'abcdef';
//console.log(file.name + ' => ' + storeAs);
console.log('file' + ' = ' + JSON.stringify(file));
var client = new OSS.Wrapper({
accessKeyId:,
accessKeySecret:,
endpoint: 'oss-cn-shenzhen.aliyuncs.com',
bucket: 'resp1'
});
client.multipartUpload(storeAs, file)
.then(function (result) {
console.log(JSON.stringify(result));
})
.catch(function (err) {
console.log(err);
});
},function(e){
console.log("读写出现异常: " + e.message);
});
});

回梦無痕

回梦無痕 - 暂停服务

经过测试,

plus.io.convertLocalFileSystemURL( url );

先把地址转成绝对地址,赋值给一个隐藏的

<input type="file" id="file" />

然后

document.getElementById('file').addEventListener('change', function(e) {  
	var file = e.target.files[0];  
	//这个file可以上传成功oss  
});
  • bishuihanshan (作者)

    昨晚回去后没空看到这个,现在才看到,我试试看,谢谢啊


    2017-09-20 12:17

  • bishuihanshan (作者)

    怎么给<input type="file" id="file" />这个赋值呢?查了下资料,都说因为安全问题,这个是只读的,必须手动去选呢?


    2017-09-20 14:16

哈哈黑

哈哈黑 - 孤独寂寞冷

楼主解决了吗?同样的疑问。。。。file不能设置值啊

bishuihanshan

bishuihanshan (作者)

没有,后来我不用阿里云的oss了,改七牛了

everyday666

everyday666

let files = file.file,
point = files.name.lastIndexOf('.'),
suffix = files.name.substr(point),
fileName = files.name.substr(0, point),
date = Date.parse(new Date()),
fileNames = ${fileName}_${date}${suffix};

client(this.dataObj).multipartUpload(fileNames, file.file).then(result => {
//下面是如果对返回结果再进行处理,根据项目需要
// console.log(result)

}).catch(err => {

})

该问题目前已经被锁定, 无法添加新回复