示例#1
0
        public async Task <bool> UploadToGoogleCloudStorage(string bucketName, string token, string filePath, string contentType)
        {
            var newObject = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket = bucketName,
                Name   = System.IO.Path.GetFileNameWithoutExtension(filePath)
            };
            var service = new Google.Apis.Storage.v1.StorageService();

            try
            {
                using (var fileStream = new FileStream(filePath, FileMode.Open))
                {
                    var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, bucketName, fileStream, contentType);
                    uploadRequest.OauthToken       = token;
                    uploadRequest.ProgressChanged += UploadProgress;
                    uploadRequest.ChunkSize        = (256 * 1024);
                    await uploadRequest.UploadAsync().ConfigureAwait(false);

                    service.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            return(true);
        }
示例#2
0
        //// PUT api/logins/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}
        //// DELETE api/login/5
        //public void Delete(int id)
        //{
        //}
        private void sendPicture()
        {
            String serviceAccountEmail = "*****@*****.**";

            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data/MyFirstProject-db5185b5746d.p12");
            var certificate = new X509Certificate2(filePath, "notasecret", X509KeyStorageFlags.Exportable);

            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                {
                    Scopes = new[] { Google.Apis.Storage.v1.StorageService.Scope.DevstorageFullControl }
                }.FromCertificate(certificate));

            Google.Apis.Storage.v1.StorageService ss = new Google.Apis.Storage.v1.StorageService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "My First Project",
            });

            var fileobj = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket = "keepguarantee",
                Name = "file"
            };

            //FileStream fileStream = null;
            //var dir = Directory.GetCurrentDirectory();
            //var path = Path.Combine(dir, "test.png");
            //fileStream = new FileStream(path, FileMode.Open);

            //var insmedia = new ObjectsResource.InsertMediaUpload(ss, fileobj, "keepguarantee", fileStream, "image/jpeg");
            //insmedia.Upload();
        }