簡體   English   中英

已創建 Blob,但未使用 SignedURL 上傳文件

[英]Blob is created but File is not uploaded using SignedURL

我正在使用 SignedURL 將文件直接上傳到 Cloud Storage,而無需通過 App Engine 實例。 我遵循的過程是:

  1. 創建一個空對象並為該對象生成一個 SignedURL

     Storage storage = null; try{ FileInputStream credentialsStream = new FileInputStream("JSONFile"); Credentials credentials = GoogleCredentials.fromStream(credentialsStream); storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService(); }catch(IOException e) { e.printStackTrace(); } Acl aclObject = Acl.of(User.ofAllUsers(),Role.OWNER); List<Acl> aclAccess = new ArrayList<>(); aclAccess.add(aclObject); //BucketName and User name are Strings. BlobId blobId = BlobId.of(BUCKET_NAME,USER_NAME+"TeamLogo"); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setAcl(aclAccess).setContentType("image/jpeg").build(); Blob blob = storage.create(blobInfo);

至此,在 Cloud Storage 中創建了一個空對象

在 Cloud Storage 中創建的對象

我正在為這個空對象生成一個簽名 URL,這樣當用戶上傳文件時,文件的內容會替換空對象

    HttpMethod httpMethod = HttpMethod.PUT;

    ServiceAccountSigner signer = ServiceAccountCredentials.newBuilder().setClientId(CLIENT_ID).setClientEmail(CLIENT_EMAIL).setPrivateKey(PRIVATEKEY).setPrivateKeyId(PRIVATE_KEY_ID).build()

    URL url = blob.signUrl(10,TimeUnit.MINUTES,Storage.SignUrlOption.httpMethod(httpMethod),Storage.SignUrlOption.signWith(signer),Storage.SignUrlOption.withContentType());
    return url;

我處理文件上傳的 HTML 代碼

 <form action="${signedURL}" method="put" enctype="multipart/form-data"> <label>Enter Your User Name</label><br> <input type="text" name="UserName" ><br><br> <label>Enter Your Team Name</label><br> <input type="text" name="TeamName" ><br><br> <label>Upload Team Logo</label><br> <input type="file" name="myFile" required="required"><br><br> <input type="submit" value="Create Team"> <input type="hidden" name="success_action_redirect" value="http://localhost:8080/register"> </form>

選擇文件並點擊上傳后,我選擇的文件沒有上傳到雲存儲,它正在加載這個頁面(一個帶有 URL 的白頁)。

輸出圖像

我沒有達到我想要的結果。 我的代碼中缺少什么? Cloud Storage 的 Java 文檔並沒有為像我這樣的新手提供全貌。 有人請在這方面幫助我。

更新:對雲存儲的 Javascript AJAX 請求

 var signedURL; function uploadFile(){ var urlxhr = new XMLHttpRequest(); //To get the SignedURL from server side urlxhr.open('GET','http://localhost:8080/getsignedurl') urlxhr.send(); urlxhr.onreadystatechange = function(){ if (urlxhr.readyState == 4 && urlxhr.status == 200) { signedURL = urlxhr.responseText; var file = document.getElementById("myFile").files[0] var formData = new FormData(); formData.append('imageFile',file); var storageXhr = new XMLHttpRequest(); storageXhr.open('PUT',signedURL,true); storageXhr.onload = () => { if(storageXhr.status == 200){ alert("File Successfully Uploaded"); }else{ alert("Something went Wrong"); } }; storageXhr.onerror = () => { alert("An Error occured while Uploading file"); }; storageXhr.setRequestHeader('Content-Type',file.type); storageXhr.send(formData); } } }
 <!-- Uploading Directly to Cloud storage by AJAX Request --> <form action="#" method="put" onsubmit="uploadFile()"> <label>Select Your Team Logo</label> <input type="file" id="myFile" required="required"> <input type="submit" value="Upload File"> </form> </div>

看起來您正在嘗試使用POST Object命令但使用PUT Object命令。 例如,您正在使用success_action_redirect ,這僅適用於 POST 對象命令。 PUT Object 命令不以這種方式接受表單數據。

您可以使用帶有簽名 URL 的 PUT 對象命令,但不能以這種方式使用。 相反,您將使用 JavaScript 來制作文件內容的上傳。

如果您打算使用 HTML 表單,那么 POST 對象命令就是您想要的命令。 不過,簽署這些請求的規則略有不同。 查看文檔的“政策文檔”部分: https : //cloud.google.com/storage/docs/xml-api/post-object#usage_and_examples

因此,要更改上面的示例,請將方法切換為POST ,添加由服務器制作的policy字段和Signature字段,與您現在簽署請求的方式相同,然后將操作切換為 ' https://storage .googleapis.com/bucket_name '。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM