0

我有一個網站(在asp.net中),我希望用戶通過該網站將照片上傳到Google雲端存儲分區。如果需要,可以使用Google對上傳的用戶進行身份驗證(儘管如果他們不是這樣的話,我會更喜歡它 - 該網站被用戶名/密碼/驗證碼鎖定)。試圖將文件從網頁上傳到Google雲端存儲

略有不相關 - 只要有鏈接(我們的一些客戶有IT部門拒絕他們使用Google帳戶,但我們不能改變他們的想法)。我希望在照片上傳時理想地返回鏈接。

我有以下的Javascript代碼,我認爲應該工作(基本上它是從here拍攝):

<script type="text/javascript" src="https://apis.google.com/js/client:plusone.js"></script> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
    <script type="text/javascript" src="https://apis.google.com/js/client.js"></script> 
    <script type="text/javascript"> 

var PROJECT = 'MY_PROJECT'; 
var clientId = 'MY_CLIENT_ID_ENDING_IN_apps.googleusercontent.com'; 
var apiKey = 'MY_API_KEY'; 
var scopes = 'https://www.googleapis.com/auth/devstorage.read_write'; 
//quick question - I've got a photo in the bucket already, and its 
//URL points to a v1_internal folder. Would that mean that the API 
//version is v1_internal? 
var API_VERSION = 'v1'; 
var BUCKET = 'MY_BUCKET'; 
var object = ""; 
//question - when using a specific group, should this read group-blahblah 
//or, for instance, owners-blahblah 
//or even group-owners-blahblah? 
var GROUP = 'group-MY_LONG_GROUP_ID'; 
//stuck on these next few ones 
var ENTITY = 'group-Owners'; 
var ROLE = 'OWNER'; 
var ROLE_OBJECT = 'OWNER'; 

function insertObject(event) { 
      try { 
       var fileData = event.target.files[0]; 
      } 
      catch(e) { 
       //'Insert Object' selected from the API Commands select list 
       //Display insert object button and then exit function 
       //filePicker.style.display = 'block'; 
       return; 
      } 
      var boundary = '-------314159265358979323846'; 
      var delimiter = "\r\n--" + boundary + "\r\n"; 
      var close_delim = "\r\n--" + boundary + "--"; 

      var reader = new FileReader(); 
      reader.readAsBinaryString(fileData); 
      reader.onload = function(e) { 
       var contentType = fileData.type || 'application/octet-stream'; 
       var metadata = { 
        'name': fileData.name, 
        'mimeType': contentType 
       }; 

       var base64Data = btoa(reader.result); 
       var multipartRequestBody = 
        delimiter + 
        'Content-Type: application/json\r\n\r\n' + 
        JSON.stringify(metadata) + 
        delimiter + 
        'Content-Type: ' + contentType + '\r\n' + 
        'Content-Transfer-Encoding: base64\r\n' + 
        '\r\n' + 
        base64Data + 
        close_delim; 

       //Note: gapi.client.storage.objects.insert() can only insert 
       //small objects (under 64k) so to support larger file sizes 
       //we're using the generic HTTP request method gapi.client.request() 
       var request = gapi.client.request({ 
        'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o', 
        'method': 'POST', 
        'params': {'uploadType': 'multipart'}, 
        'headers': { 
         'Content-Type': 'multipart/mixed; boundary="' + boundary + '"' 
        }, 
        'body': multipartRequestBody}); 
       //Remove the current API result entry in the main-content div 
       //listChildren = document.getElementById('main-content').childNodes; 
       //if (listChildren.length > 1) { 
       // listChildren[1].parentNode.removeChild(listChildren[1]); 
       //} 

       //look at http://stackoverflow.com/questions/30317797/uploading-additional-metadata-as-part-of-file-upload-request-to-google-cloud-sto 

       try{ 
        //Execute the insert object request 
        executeRequest(request, 'insertObject'); 
        //Store the name of the inserted object 
        object = fileData.name;   
       } 
       catch(e) { 
        alert('An error has occurred: ' + e.message); 
       } 
      } 
     } 

</script> 

目前,我可以執行的功能。該文件在函數內部結束(我已經能夠調用fileData.name上的警報)。但是,它並沒有結束,並且沒有提出錯誤信息。

此代碼看起來好嗎,還是存儲桶的設置方式有問題?我是否使用了正確的值(或者我是否正確地格式化了它們)?

回答

0

對其進行排序。對於那些嘗試過使用此代碼的人,並且像我一樣,沒有費心閱讀,Google正在使用「授權」按鈕。基本上,他們提供的示例代碼使您可以或多或少地「附加」文件,然後是另一個可以進行實際上傳的按鈕。我自己沒有看到這個。

相關問題