视频上传到使用钛的谷歌云存储

时间:2018-06-05 06:22:27

标签: file-upload titanium google-cloud-storage appcelerator hybrid-mobile-app

我正在使用Appcelerator来构建应用程序。我遇到了我想将录制的视频从我的应用上传到Google云端存储的情况。我们正在寻找用于上传的谷歌云存储sdk或模块,但我们找不到多少。我已经尝试了几种方法,我已经研究过

方法1:

var dataUri = event.media.nativePath;
Titanium.Media.saveToPhotoGallery(dataUri);
xhr.onload = function(e) {
Ti.API.info('onload:- ' + JSON.stringify(this.responseText));
};
xhr.onerror = function(e) {
Ti.API.info('onerror:- ' + JSON.stringify(this.responseText));
  };
xhr.onsendstream = function(e) {
Ti.API.info('math:- ' + Math.floor(e.progress * 100) + "%");
};

xhr.open('GET',
'https://www.googleapis.com/storage/v1/b/bucket_name/o?’);
xhr.onsendstream = function(e) {
   Ti.API.info('math:- ' + Math.floor(e.progress * 100) + "%");
};
xhr.send({ file : dataUri,key:'API KEY' });

回应: -

   {
   "error": {
    "errors": [{
        "domain": "usageLimits",
        "reason": "ipRefererBlocked",
        "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
        "extendedHelp": "https://cosole.developers.google.com"  
               }],
    "code": 403,
    "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."}
  }

方法2:

   var dataUri = event.media.nativePath;
  Titanium.Media.saveToPhotoGallery(dataUri);
  xhr.onload = function(e) {
    Ti.API.info('onload:- ' + JSON.stringify(this.responseText));
  };
  xhr.onerror = function(e) {
     Ti.API.info('onerror:- ' + JSON.stringify(this.responseText));
   };
  xhr.onsendstream = function(e) {
     Ti.API.info('math:- ' + Math.floor(e.progress * 100) + "%");
  };
  var REQUEST_URL = "https://www.googleapis.com/auth/devstorage.full_control"; 
 xhr.open("GET", REQUEST_URL);
 xhr.onsendstream = function(e) {
 Ti.API.info('math:- ' + Math.floor(e.progress * 100) + "%");
};
xhr.send();

回复: - devstorage.full_control

方法3:

var dataUri = event.media.nativePath;
Titanium.Media.saveToPhotoGallery(dataUri);
var xhr = Titanium.Network.createHTTPClient({
enableKeepAlive : false
});
xhr.send({ video : dataUri });

xhr.setRequestHeader("enctype", "multipart/form-data");
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.onerror = function(e) {
alert(e.error);
};

xhr.onload = function() {
var data = JSON.parse(this.responseText);
if (data.FILE)
  alert('File: ' + data.FILE);
else
  alert(this.responseText);
};
xhr.open('GET', ‘ipaddress/api_file/api_name’);
xhr.send();

回应: -

{
    "result": {
    "errno": -4058,
    "code": "ENOENT",
    "syscall": "stat",
    "path": "C:\Sites\Api\test\\file:\\\storage\\emulated\0\Pictures\\test-966434132.mp4"
  },
    "status": 2
}

请在这种情况下帮助我,我对此很新。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我进行了搜索,找不到任何专门用Titanium编写的库,用于上传到Google云端存储。

所以你基本上有2个选择:

第一种是使用本机客户端库(官方或某人已编写过的)并使用Titanium模块进行包装。您可以在此处找到客户端库(https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-java

第二种选择是自己直接拨打云存储json api。可以在此处找到API文档:https://cloud.google.com/storage/docs/json_api/,您需要使用Titanium代码(https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.HTTPClient)实现对它的http调用。

为了便于编写http API,我推荐使用Jason Keen的RESTe库(https://github.com/jasonkneen/RESTe

相关问题