public override UploadResult Upload(Stream stream, string fileName) { if (!CheckAuthorization()) { return(null); } string uploadPath = GetUploadPath(fileName); OnEarlyURLCopyRequested(GenerateURL(uploadPath)); GoogleCloudStorageMetadata googleCloudStorageMetadata = new GoogleCloudStorageMetadata { name = uploadPath, acl = new GoogleCloudStorageAcl[] { new GoogleCloudStorageAcl { entity = "allUsers", role = "READER" } } }; string serializedGoogleCloudStorageMetadata = JsonConvert.SerializeObject(googleCloudStorageMetadata); UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart&fields=name", stream, fileName, null, headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: serializedGoogleCloudStorageMetadata); GoogleCloudStorageResponse googleCloudStorageResponse = JsonConvert.DeserializeObject <GoogleCloudStorageResponse>(result.Response); result.URL = GenerateURL(googleCloudStorageResponse.name); return(result); }
public override UploadResult Upload(Stream stream, string fileName) { if (!CheckAuthorization()) { return(null); } string name = fileName; if ((RemoveExtensionImage && Helpers.IsImageFile(fileName)) || (RemoveExtensionText && Helpers.IsTextFile(fileName)) || (RemoveExtensionVideo && Helpers.IsVideoFile(fileName))) { name = Path.GetFileNameWithoutExtension(fileName); } string uploadpath = GetUploadPath(name); GoogleCloudStorageMetadata metadata = new GoogleCloudStorageMetadata { name = uploadpath, acl = new GoogleCloudStorageAcl[] { new GoogleCloudStorageAcl { entity = "allUsers", role = "READER" } } }; string metadatajson = JsonConvert.SerializeObject(metadata); UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart", stream, fileName, "file", headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: metadatajson); GoogleCloudStorageResponse upload = JsonConvert.DeserializeObject <GoogleCloudStorageResponse>(result.Response); if (upload.name != uploadpath) { Errors.Add("Upload failed."); return(null); } result.URL = GenerateURL(uploadpath); return(result); }