Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            string uploadpath = GetUploadPath(fileName);

            if (string.IsNullOrEmpty(Domain))
            {
                Domain = $"storage.googleapis.com/{Bucket}";
            }

            Metadata metadata = new Metadata
            {
                name = uploadpath,
                acl  = new Acl[]
                {
                    new Acl
                    {
                        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,
                                                  headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadatajson);
            GoogleCloudStorageResponse upload = JsonConvert.DeserializeObject <GoogleCloudStorageResponse>(result.Response);

            if (upload.name != uploadpath)
            {
                Errors.Add("Upload failed.");
                return(null);
            }

            result.URL = URLHelpers.FixPrefix($"{Domain}/{uploadpath}", "https://");

            return(result);
        }