public void PutScreenshot(Amazon.S3.AmazonS3 amazonS3Client, string bucketName, string path, Stream stream)
        {
            var putObjectRequest = new PutObjectRequest();

            putObjectRequest.WithInputStream(stream);
            putObjectRequest.WithBucketName(bucketName);
            putObjectRequest.WithKey(path);

            amazonS3Client.PutObject(putObjectRequest);
        }
示例#2
0
 public static string CreateNewFolder(AmazonS3 client, string foldername)
 {
     String S3_KEY = foldername;
     PutObjectRequest request = new PutObjectRequest();
     request.WithBucketName(BUCKET_NAME);
     request.WithKey(S3_KEY);
     request.WithContentBody("");
     client.PutObject(request);
     return S3_KEY;
 }
示例#3
0
 public static string CreateNewFileInFolder(AmazonS3 client, string foldername, string filepath)
 {
     String S3_KEY = foldername + "/" + System.IO.Path.GetFileName(filepath);
     PutObjectRequest request = new PutObjectRequest();
     request.WithBucketName(BUCKET_NAME);
     request.WithKey(S3_KEY);
     request.WithFilePath(filepath);
     //request.WithContentBody("This is body of S3 object.");
     client.PutObject(request);
     return S3_KEY;
 }
示例#4
0
 /// <summary>
 /// The save file.
 /// </summary>
 /// <param name="awsAccessKey">
 /// The AWS access key.
 /// </param>
 /// <param name="awsSecretKey">
 /// The AWS secret key.
 /// </param>
 /// <param name="bucket">
 /// The bucket.
 /// </param>
 /// <param name="path">
 /// The path.
 /// </param>
 /// <param name="fileStream">
 /// The file stream.
 /// </param>
 public static void SaveFile(string awsAccessKey, string awsSecretKey, string bucket, string path, Stream fileStream)
 {
     using (var amazonClient = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey))
     {
         var createFileRequest = new PutObjectRequest
                                     {
                                         CannedACL = S3CannedACL.PublicRead,
                                         Timeout = int.MaxValue
                                     };
         createFileRequest.WithKey(path);
         createFileRequest.WithBucketName(bucket);
         createFileRequest.WithInputStream(fileStream);
         amazonClient.PutObject(createFileRequest);
     }
 }
         public bool UploadFileToS3Object(string s3ObjectName, string filePath)
         {

             try
             {
                 PutObjectRequest request = new PutObjectRequest();
                 request.WithBucketName(bucketName);
                 request.WithKey(s3ObjectName);
                 request.WithFilePath(filePath);//request.WithContentBody
                 amazonS3Client.PutObject(request);
                 return true;
             }
             catch (Exception e)
             {
                 structuredLog("E", "Exception in UploadFileToS3Object: "+e);
                 return false;
             }

         }
示例#6
0
        private void UploadEvents(LoggingEvent[] events, AmazonS3 client)
        {
            var key = Filename(Guid.NewGuid().ToString());

            var content = new StringBuilder(events.Count());
            Array.ForEach(events, logEvent =>
                {
                    using (var writer = new StringWriter())
                    {
                        Layout.Format(writer, logEvent);
                        content.AppendLine(writer.ToString());
                    }
                });

            var request = new PutObjectRequest();
            request.WithBucketName(_bucketName);
            request.WithKey(key);
            request.WithContentBody(content.ToString());
            client.PutObject(request);
        }
        public IList<string> UploadFiles(string path, IList<FileToUpload> files)
        {
            ValidatePath(path);

            var retVal = new List<string>(files.Count);

            using (AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(new AmazonS3Config().WithServiceURL("s3-us-west-1.amazonaws.com")))
            {
                string bucketName = ConfigurationManager.AppSettings[Constants.S3_BUCKET];

                foreach (FileToUpload file in files)
                {
                    var fileUploadRequest = new PutObjectRequest();

                    string key = path + Guid.NewGuid() + Path.GetExtension(file.Filename);

                    fileUploadRequest
                        .WithKey(key)
                        .WithCannedACL(S3CannedACL.PublicRead)
                        .WithBucketName(bucketName)
                        .WithMetaData("Image-Info",file.MetaInfo)
                        .WithInputStream(file.Stream);

                    fileUploadRequest.StorageClass = S3StorageClass.ReducedRedundancy;

                    //"R" - RFC1123 - http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#RFC1123
                    fileUploadRequest.AddHeader("Expires", DateTime.UtcNow.AddYears(10).ToString("R"));
                    fileUploadRequest.AddHeader("Cache-Control", "public, max-age=31536000");

                    using (client.PutObject(fileUploadRequest))
                    {
                    }

                    retVal.Add(string.Format("http://{0}/{1}", bucketName, key));
                }
            }

            return retVal;
        }
		private void DoUpload(string backupPath, PeriodicBackupSetup backupConfigs)
		{
			var AWSRegion = RegionEndpoint.GetBySystemName(backupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1;

			var desc = string.Format("Raven.Database.Backup {0} {1}", Database.Name,
			                     DateTimeOffset.UtcNow.ToString("u"));

			if (!string.IsNullOrWhiteSpace(backupConfigs.GlacierVaultName))
			{
				var manager = new ArchiveTransferManager(awsAccessKey, awsSecretKey, AWSRegion);
				var archiveId = manager.Upload(backupConfigs.GlacierVaultName, desc, backupPath).ArchiveId;
				logger.Info(string.Format("Successfully uploaded backup {0} to Glacier, archive ID: {1}", Path.GetFileName(backupPath),
										  archiveId));
				return;
			}

			if (!string.IsNullOrWhiteSpace(backupConfigs.S3BucketName))
			{
				var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, AWSRegion);

				using (var fileStream = File.OpenRead(backupPath))
				{
					var key = Path.GetFileName(backupPath);
					var request = new PutObjectRequest();
					request.WithMetaData("Description", desc);
					request.WithInputStream(fileStream);
					request.WithBucketName(backupConfigs.S3BucketName);
					request.WithKey(key);

					using (S3Response _ = client.PutObject(request))
					{
						logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
							Path.GetFileName(backupPath), backupConfigs.S3BucketName, key));
						return;
					}
				}
			}
		}
示例#9
0
文件: ACL.cs 项目: kingbike/NetSDK
        public static void ACLSerial(String filePath)
        {
            System.Console.WriteLine("\nhello,ACL!!");
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"]);
            String bucketName = "chutest";
            String objectName = "hello";
            //versioning test
            String vbucketName = "netversion";

            //**********************************************************************************************************************************************************
            // Set Version test 環境,目前新增一專用 bucket = netversion,內有兩筆 Object 資訊(Object 已刪除,帶有 delete marker),為了測試方便,此環境不共用也不刪除
            // 若於佈板後刪除環境,請預先建立環境,執行132~150行程式
            //**********************************************************************************************************************************************************

            //PutBucket
            /*    System.Console.WriteLine("PutBucket-version: {0}\n", vbucketName);
                s3Client.PutBucket(new PutBucketRequest().WithBucketName(vbucketName));

                //PutBucketVersioning
                SetBucketVersioningResponse putVersioningResult = s3Client.SetBucketVersioning(new SetBucketVersioningRequest().WithBucketName(vbucketName).WithVersioningConfig(new S3BucketVersioningConfig().WithStatus("Enabled")));
                System.Console.WriteLine("PutBucketVersioning, requestID:{0}\n", putVersioningResult.RequestId);

                //PutObject
                System.Console.WriteLine("PutObject!");
                PutObjectRequest objectVersionRequest = new PutObjectRequest();
                objectVersionRequest.WithBucketName(vbucketName);
                objectVersionRequest.WithKey(objectName);
                objectVersionRequest.WithFilePath(filePath);
                PutObjectResponse objectVersionResult = s3Client.PutObject(objectVersionRequest);
                System.Console.WriteLine("Uploaded Object Etag: {0}\n", objectVersionResult.ETag);

                //DeleteObject
                System.Console.WriteLine("Delete Object!");
                s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(vbucketName).WithKey(objectName));
            */

            //PutBucket
            System.Console.WriteLine("PutBucket: {0}\n", bucketName);
            s3Client.PutBucket(new PutBucketRequest().WithBucketName(bucketName));

            //PutObject
            System.Console.WriteLine("PutObject!");
            PutObjectRequest request = new PutObjectRequest();
            request.WithBucketName(bucketName);
            request.WithKey(objectName);
            request.WithFilePath(filePath);
            PutObjectResponse PutResult = s3Client.PutObject(request);
            System.Console.WriteLine("Uploaded Object Etag: {0}\n", PutResult.ETag);

            //PutBucketACL
            SetACLRequest aclRequest = new SetACLRequest();
            S3AccessControlList aclConfig = new S3AccessControlList();
            aclConfig.WithOwner(new Owner().WithDisplayName("hrchu").WithId("canonicalidhrchu"));
            aclConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidhrchu","hrchu")).WithPermission(S3Permission.FULL_CONTROL));
            aclConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidannyren", "annyren")).WithPermission(S3Permission.READ_ACP));

            aclRequest.WithBucketName(bucketName);
            aclRequest.WithACL(aclConfig);

            SetACLResponse putBucketACLResult = s3Client.SetACL(aclRequest);
            System.Console.WriteLine("\nPutBucketACL, requestID:{0}",putBucketACLResult.RequestId);

            //GetBucketACL
            GetACLResponse getBucketACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(bucketName));
            System.Console.WriteLine("\nGetBucketACL Result:\n{0}\n",getBucketACLResult.ResponseXml);

            //**********************************************************************************************************************************************************

            //PutBucketACL (cannedacl)
            SetACLRequest cannedaclRequest = new SetACLRequest();
            cannedaclRequest.WithBucketName(bucketName);
            cannedaclRequest.WithCannedACL(S3CannedACL.PublicRead);

            SetACLResponse putBucketCannedACLResult = s3Client.SetACL(cannedaclRequest);
            System.Console.WriteLine("\nPutBucketCannedACL, requestID:{0}", putBucketCannedACLResult.RequestId);

            //GetBucketACL
            GetACLResponse getBucketCannedACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(bucketName));
            System.Console.WriteLine("\nGetBucketCannedACL Result:\n{0}\n", getBucketCannedACLResult.ResponseXml);

            //**********************************************************************************************************************************************************

            //PutObjectACL
            SetACLRequest objectACLRequest = new SetACLRequest();
            S3AccessControlList objectACLConfig = new S3AccessControlList();
            objectACLConfig.WithOwner(new Owner().WithDisplayName("hrchu").WithId("canonicalidhrchu"));
            objectACLConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidhrchu", "hrchu")).WithPermission(S3Permission.FULL_CONTROL));
            objectACLConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidannyren", "annyren")).WithPermission(S3Permission.WRITE_ACP));

            objectACLRequest.WithBucketName(bucketName);
            objectACLRequest.WithKey(objectName);
            objectACLRequest.WithACL(objectACLConfig);

            SetACLResponse putObjectACLResult = s3Client.SetACL(objectACLRequest);
            System.Console.WriteLine("\nPutObjectACL, requestID:{0}", putObjectACLResult.RequestId);

            //GetObjectACl
            GetACLResponse getObjectACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(bucketName).WithKey(objectName));
            System.Console.WriteLine("\nGetObjectACL Result:\n{0}\n", getObjectACLResult.ResponseXml);

            //**********************************************************************************************************************************************************

            //PutObjectACL (cannedacl)
            SetACLRequest objectCannedACLRequest = new SetACLRequest();

            objectCannedACLRequest.WithBucketName(bucketName);
            objectCannedACLRequest.WithKey(objectName);
            objectCannedACLRequest.WithCannedACL(S3CannedACL.PublicRead);

            SetACLResponse putObjectCannedACLResult = s3Client.SetACL(objectCannedACLRequest);
            System.Console.WriteLine("\nPutObjectCannedACL, requestID:{0}", putObjectCannedACLResult.RequestId);

            //GetObjectACl
            GetACLResponse getObjectCannedACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(bucketName).WithKey(objectName));
            System.Console.WriteLine("\nGetObjectCannedACL Result:\n{0}\n", getObjectCannedACLResult.ResponseXml);

            //**********************************************************************************************************************************************************

            //DeleteObject
            System.Console.WriteLine("Delete Object!");
            s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(objectName));
            //DeleteBucket
            System.Console.WriteLine("Delete Bucket!");
            s3Client.DeleteBucket(new DeleteBucketRequest().WithBucketName(bucketName));

            //PutObjectACL-version test*********************************************************************************************************************************
            String versionid = "784cca47e2a7423f97dedde6a72f9b3d";

            //PutObjectACL-versionid
              SetACLRequest objectVersionACLRequest = new SetACLRequest();
              S3AccessControlList objectVersionACLConfig = new S3AccessControlList();
              objectVersionACLConfig.WithOwner(new Owner().WithDisplayName("hrchu").WithId("canonicalidhrchu"));
              objectVersionACLConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidhrchu", "hrchu")).WithPermission(S3Permission.FULL_CONTROL));
              objectVersionACLConfig.WithGrants(new S3Grant().WithGrantee(new S3Grantee().WithCanonicalUser("canonicalidannyren", "annyren")).WithPermission(S3Permission.WRITE_ACP));

              objectVersionACLRequest.WithBucketName(vbucketName);
              objectVersionACLRequest.WithKey(objectName);
              objectVersionACLRequest.WithVersionId(versionid);
              objectVersionACLRequest.WithACL(objectVersionACLConfig);

              SetACLResponse putObjectVersionACLResult = s3Client.SetACL(objectVersionACLRequest);
              System.Console.WriteLine("\nPutObjectACL Version, requestID:{0}", putObjectVersionACLResult.RequestId);

              //GetObjectACl-versionid
              GetACLResponse getObjectVersionACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(vbucketName).WithKey(objectName).WithVersionId(versionid));
              System.Console.WriteLine("\nGetObjectACL Version Result:\n{0}\n", getObjectVersionACLResult.ResponseXml);

              //PutObjectACL (cannedacl)-versionid
              SetACLRequest objectVersionCannedACLRequest = new SetACLRequest();

              objectVersionCannedACLRequest.WithBucketName(vbucketName);
              objectVersionCannedACLRequest.WithKey(objectName);
              objectVersionCannedACLRequest.WithVersionId(versionid);
              objectVersionCannedACLRequest.WithCannedACL(S3CannedACL.PublicRead);

              SetACLResponse putObjectVersionCannedACLResult = s3Client.SetACL(objectVersionCannedACLRequest);
              System.Console.WriteLine("\nPutObjectCannedACL Version, requestID:{0}", putObjectVersionCannedACLResult.RequestId);

              //GetObjectACl(cannedacl)-versionid
              GetACLResponse getObjectVersionCannedACLResult = s3Client.GetACL(new GetACLRequest().WithBucketName(vbucketName).WithKey(objectName).WithVersionId(versionid));
              System.Console.WriteLine("\nGetObjectCannedACL Version Result:\n{0}\n", getObjectVersionCannedACLResult.ResponseXml);

            System.Console.WriteLine("END!");
        }
示例#10
0
        /*
         Sample call for upload:-
            byte[] array = new byte[1024*1024*1024];
            Random random = new Random();
            random.NextBytes(array);
            double timeTaken_Upload = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Upload, "fooContainer", "fooBlob");
            double timeTaken_Download = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Download, "fooContainer", "fooBlob");
         *
         *
         */
        public static double doRawCloudPerf(byte[] input, SynchronizerType synchronizerType, 
            SynchronizeDirection syncDirection, string exp_directory, Logger logger, string containerName=null, string blobName=null)
        {
            string accountName = ConfigurationManager.AppSettings.Get("AccountName");
            string accountKey = ConfigurationManager.AppSettings.Get("AccountSharedKey");

            DateTime begin=DateTime.Now, end=DateTime.Now;

            if (synchronizerType == SynchronizerType.Azure)
            {
                #region azure download/upload
                if (containerName==null)
                    containerName = "testingraw";
                if(blobName==null)
                    blobName = Guid.NewGuid().ToString();

                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), true);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                if (syncDirection == SynchronizeDirection.Upload)
                {
                    logger.Log("Start Stream Append");
                    container.CreateIfNotExist();
                    begin = DateTime.UtcNow;//////////////////////////////////////
                    try
                    {
                        using (MemoryStream memoryStream = new System.IO.MemoryStream(input))
                        {
                            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                            blockBlob.UploadFromStream(memoryStream);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                    end = DateTime.UtcNow;//////////////////////////////////////
                    logger.Log("End Stream Append");
                }

                if (syncDirection == SynchronizeDirection.Download)
                {
                    logger.Log("Start Stream Get");
                    logger.Log("Start Stream GetAll");
                    try
                    {
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                        byte[] blobContents = blockBlob.DownloadByteArray();

                        //if (File.Exists(blobName))
                        //   File.Delete(blobName);

                        begin = DateTime.UtcNow;//////////////////////////////////////
                        // using (FileStream fs = new FileStream(blobName, FileMode.OpenOrCreate))
                        // {
                            byte[] contents = blockBlob.DownloadByteArray();
                            // fs.Write(contents, 0, contents.Length);
                        // }
                    }
                    catch (Exception e)
                    {
                    }
                    end = DateTime.UtcNow;//////////////////////////////////////
                    logger.Log("End Stream Get");
                    logger.Log("End Stream GetAll");

                }
                #endregion

            }

            else if (synchronizerType == SynchronizerType.AmazonS3)
            {
                #region amazon s3 stuff
                if (containerName == null)
                    containerName = "testingraw";
                if (blobName == null)
                    blobName = Guid.NewGuid().ToString();

                AmazonS3Client amazonS3Client = new AmazonS3Client(accountName, accountKey);
                if (syncDirection == SynchronizeDirection.Upload)
                {
                    ListBucketsResponse response = amazonS3Client.ListBuckets();
                    foreach (S3Bucket bucket in response.Buckets)
                    {
                        if (bucket.BucketName == containerName)
                        {
                            break;
                        }
                    }
                    amazonS3Client.PutBucket(new PutBucketRequest().WithBucketName(containerName));

                    begin = DateTime.UtcNow;//////////////////////////////////////
                    MemoryStream ms = new MemoryStream();
                    ms.Write(input, 0, input.Length);
                    PutObjectRequest request = new PutObjectRequest();
                    request.WithBucketName(containerName);
                    request.WithKey(blobName);
                    request.InputStream = ms;
                    amazonS3Client.PutObject(request);
                    end = DateTime.UtcNow;//////////////////////////////////////

                }

                if (syncDirection == SynchronizeDirection.Download)
                {
                    if (File.Exists(blobName))
                        File.Delete(blobName);

                    begin = DateTime.UtcNow;//////////////////////////////////////
                    GetObjectRequest request = new GetObjectRequest();
                    request.WithBucketName(containerName);
                    request.WithKey(blobName);
                    GetObjectResponse response = amazonS3Client.GetObject(request);
                    var localFileStream = File.Create(blobName);
                    response.ResponseStream.CopyTo(localFileStream);
                    localFileStream.Close();
                    end = DateTime.UtcNow;//////////////////////////////////////
                }
            #endregion
            }
            else
            {
                throw new InvalidDataException("syncronizer type is not valid");
            }

            return (end - begin).TotalMilliseconds;// return total time to upload in milliseconds
        }
         public bool UploadByteArrayToS3Object(string s3ObjectName, byte[] input)
         {
             try
             {
                 MemoryStream ms = new MemoryStream();
                 ms.Write(input, 0, input.Length);
                 PutObjectRequest request = new PutObjectRequest();
                 request.WithBucketName(bucketName);
                 request.WithKey(s3ObjectName);
                 request.InputStream = ms;
                 amazonS3Client.PutObject(request);
                 return true;
             }
             catch (Exception e)
             {
                 structuredLog("E", "Exception in UploadFileToS3Object: " + e);
                 return false;
             }

         }
示例#12
0
        private void UploadToAmazonService(HttpPostedFileBase file, string filename)
        {
            string bucketName = System.Configuration.ConfigurationManager.AppSettings["AWSPublicFilesBucket"]; //commute bucket

            string publicFile = "Pictures/" + filename; //We have Pictures folder in the bucket
            Session["publicFile"] = publicFile;

            PutObjectRequest request = new PutObjectRequest();
            request.WithBucketName(bucketName);
            request.WithKey(publicFile);
            request.WithInputStream(file.InputStream);
            request.AutoCloseStream = true;
            request.CannedACL = S3CannedACL.PublicRead; //Read access for everyone

            AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(); //uses AWSAccessKey and AWSSecretKey defined in Web.config
            using (S3Response r = client.PutObject(request)) { }
        }
示例#13
0
 public static string UploadFile(AmazonS3 client, string filepath)
 {
     //S3_KEY is name of file we want upload
     S3_KEY = System.IO.Path.GetFileName(filepath);
     PutObjectRequest request = new PutObjectRequest();
     request.WithBucketName(BUCKET_NAME);
     request.WithKey(S3_KEY);
     //request.WithInputStream(MemoryStream);
     request.WithFilePath(filepath);
     client.PutObject(request);
     return S3_KEY;
 }
示例#14
0
        private static void UploadFile(string filePath, string key, string bucket)
        {
            string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
            string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];

            using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey))
            {
                PutObjectRequest request = new PutObjectRequest();
                request.WithBucketName(bucket);
                request.WithKey(key);
                request.WithFilePath(filePath);
                request.CannedACL = S3CannedACL.PublicRead;

                client.PutObject(request);
            }
        }
示例#15
0
文件: EtlToS3.cs 项目: agrc/tile-etl
        private static void CopyImage(object o)
        {
            UserData u = (UserData)o;

              try
              {
            string imagePath = String.Format("{0}\\L{1:00}\\R{2:x8}\\C{3:x8}.{4}", TileDirectory, u.Level, u.Row, u.Column, ContentType == "image/png" ? "png" : "jpg");

            if (File.Exists(imagePath))
            {
              byte[] file = File.ReadAllBytes(imagePath);

              PutObjectRequest putObjectRequest = new PutObjectRequest();
              putObjectRequest.WithBucketName(BucketName);
              putObjectRequest.WithKey(String.Format("{0}/{1}/{2}/{3}", MapName, u.Level, u.Row, u.Column));
              putObjectRequest.WithInputStream(new MemoryStream(file));
              putObjectRequest.WithContentType(ContentType);
              putObjectRequest.WithCannedACL(S3CannedACL.PublicRead);

              _s3Client.PutObject(putObjectRequest);
            }
              }
              catch (Exception ex)
              {
              }
              finally
              {
            _threadPool.Release();
              }
        }
示例#16
0
文件: Object.cs 项目: kingbike/NetSDK
        public static void objectSerial(String filePath)
        {
            System.Console.WriteLine("\nhello,object!!");
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"]);
            String bucketName = "chttest3";
            String objectName = "hello";
            //String filePath = "D:\\Visual Studio Project\\TestNetSDK\\TestNetSDK\\pic.jpg";

            //PutBucket
            System.Console.WriteLine("PutBucket: {0}\n",bucketName);
            PutBucketResponse response = s3Client.PutBucket(new PutBucketRequest().WithBucketName(bucketName));

            //PutObject
            System.Console.WriteLine("PutObject!\n");
            PutObjectRequest request = new PutObjectRequest();
            request.WithBucketName(bucketName);
            request.WithKey(objectName);
            request.WithFilePath(filePath);
            PutObjectResponse PutResult = s3Client.PutObject(request);
            System.Console.WriteLine("Uploaded Object Etag: {0}\n", PutResult.ETag);

            //HeadObject
            System.Console.WriteLine("HeadObject!\n");
            GetObjectMetadataResponse HeadResult = s3Client.GetObjectMetadata(new GetObjectMetadataRequest().WithBucketName(bucketName).WithKey(objectName));
            System.Console.WriteLine("HeadObject: (1)ContentLength: {0} (2)ETag: {1}\n", HeadResult.ContentLength,HeadResult.ETag);

            //GetObject
            System.Console.WriteLine("GetObject!\n");
            GetObjectResponse GetResult =  s3Client.GetObject(new GetObjectRequest().WithBucketName(bucketName).WithKey(objectName).WithByteRange(1,15));

            Stream responseStream = GetResult.ResponseStream;
            StreamReader reader = new StreamReader(responseStream);
            System.Console.WriteLine("Get Object Content:\n {0}\n", reader.ReadToEnd());

            System.Console.WriteLine("Get Object ETag:\n {0}\n", GetResult.ETag);

            //CopyObject
            CopyObjectResponse CopyResult = s3Client.CopyObject(new CopyObjectRequest().WithSourceBucket(bucketName).WithSourceKey(objectName).WithDestinationBucket(bucketName).WithDestinationKey("hihi"));
            System.Console.WriteLine("CopyObject: ETag: {0} \n",CopyResult.ETag);

            //DeleteObject
            System.Console.WriteLine("Delete Object!\n");
            s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(objectName));
            s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("hihi")); //copied object

            //==============================jerry add ==============================
            System.Console.WriteLine("Jerry Add!\n");
            String objectName2 = "hello2";
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("A", "ABC");

            System.Console.WriteLine("PutObject!\n");
            PutObjectRequest request2 = new PutObjectRequest();
            request2.WithBucketName(bucketName);
            request2.WithKey(objectName2);
            request2.WithAutoCloseStream(true);
            request2.WithCannedACL(S3CannedACL.BucketOwnerFullControl);
            request2.WithContentBody("test");
            request2.WithContentType("test/xml");
            request2.WithGenerateChecksum(true);
            request2.WithMD5Digest("CY9rzUYh03PK3k6DJie09g==");

            request2.WithMetaData(nvc);
            request2.WithWebsiteRedirectLocation("http://hicloud.hinet.net/");
            PutObjectResponse PutResult2 = s3Client.PutObject(request2);
            System.Console.WriteLine("Uploaded Object Etag: {0}\n", PutResult2.ETag);

            System.Console.WriteLine("GetObject!\n");
            GetObjectRequest request3 = new GetObjectRequest();
            request3.WithBucketName(bucketName);
            request3.WithKey(objectName2);
            request3.WithByteRange(1, 2);
            DateTime datetime = DateTime.UtcNow;
            // 似乎不支援 datetime
            request3.WithModifiedSinceDate(datetime.AddHours(-1));
            request3.WithUnmodifiedSinceDate(datetime.AddHours(1));
            request3.WithETagToMatch(PutResult2.ETag);
            request3.WithETagToNotMatch("notMatch");
            GetObjectResponse GetResult2 = s3Client.GetObject(request3);
            Stream responseStream2 = GetResult2.ResponseStream;
            StreamReader reader2 = new StreamReader(responseStream2);
            System.Console.WriteLine("Get Object Content(es):\n {0}\n", reader2.ReadToEnd());
            System.Console.WriteLine("Get Object ETag:\n {0}\n", GetResult2.ETag);

            System.Console.WriteLine("HeadObject!\n");
            GetObjectMetadataRequest request4 = new GetObjectMetadataRequest();
            request4.WithBucketName(bucketName);
            request4.WithKey(objectName2);
            DateTime datetime2 = DateTime.UtcNow;
            // 似乎不支援 datetime
            request4.WithModifiedSinceDate(datetime2.AddHours(-1));
            request4.WithUnmodifiedSinceDate(datetime2.AddHours(1));
            request4.WithETagToMatch(PutResult2.ETag);
            request4.WithETagToNotMatch("notMatch");
            GetObjectMetadataResponse HeadResult2 = s3Client.GetObjectMetadata(request4);
            System.Console.WriteLine("HeadObject: (1)ContentLength: {0} (2)ETag: {1}\n", HeadResult2.ContentLength, HeadResult2.ETag);

            CopyObjectRequest request5 = new CopyObjectRequest();
            request5.WithSourceBucket(bucketName);
            request5.WithSourceKey(objectName2);
            request5.WithDestinationBucket(bucketName);
            request5.WithDestinationKey("hihi2");
            DateTime datetime3 = DateTime.UtcNow;
            // 似乎不支援 datetime
            request5.WithModifiedSinceDate(datetime3.AddHours(-1));
            request5.WithUnmodifiedSinceDate(datetime3.AddHours(1));
            request5.WithETagToMatch(PutResult2.ETag);
            request5.WithETagToNotMatch("notMatch");
            request5.WithMetaData(nvc);
            request5.WithCannedACL(S3CannedACL.PublicRead);
            request5.WithContentType("test/xml");
            request5.WithWebsiteRedirectLocation("http://hicloud.hinet.net/");
            CopyObjectResponse CopyResult2 = s3Client.CopyObject(request5);
            System.Console.WriteLine("CopyObject: ETag: {0} \n", CopyResult2.ETag);

            System.Console.WriteLine("Delete Object!\n");
            s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(objectName2));
            s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("hihi2")); //copied object
            //==============================jerry add end==============================

            //DeleteBucket
            System.Console.WriteLine("Delete Bucket!\n");
            //s3Client.DeletesBucket(new DeleteBucketRequest().WithBucketName(bucketName));
            s3Client.DeleteBucket(new DeleteBucketRequest().WithBucketName(bucketName));
            System.Console.WriteLine("END!");
        }
示例#17
0
        public ActionResult UploadFile(FormCollection formCollection)
        {
            var hpf = Request.Files["image"];
            string accessKey = System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"];
            string secretAccessKey = System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"];
            string bucketName = System.Configuration.ConfigurationManager.AppSettings["AWSPublicFilesBucket"];

            string publicFile =  hpf.FileName;

            PutObjectRequest request = new PutObjectRequest();
            request.WithBucketName(bucketName);
            request.WithKey(publicFile);

            request.WithInputStream(hpf.InputStream);
            request.AutoCloseStream = true;
            request.CannedACL = S3CannedACL.PublicRead;

            AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey);

            using (S3Response r = client.PutObject(request)) {

                foreach (var key in r.Headers.AllKeys)
                {
                    log.DebugFormat("header key {0} value{1}", key, r.Headers[key]);
                }

                ViewBag.Response= r;

            }

            var images = session.Query<Image>();
            return View(images);
        }
示例#18
0
        public String Save(DirectoryEnum denum, String localFilename)
        {
            String directory = getDirectory(denum);
            if (_storageMode == StorageMode.S3)
            {
                PutObjectRequest request = new PutObjectRequest();
                request.WithBucketName("FaceSpace");
                String s3path = directory + _directorySeparator + Path.GetFileName(localFilename);
                request.WithKey(s3path);
                request.WithFilePath(localFilename);
                request.AutoCloseStream = true;
                request.CannedACL = S3CannedACL.PublicRead;

                AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(_accessKey, _secretKey, RegionEndpoint.USEast1);
                //var client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.USEast1);
                using (S3Response r = client.PutObject(request)) { }
                return "https://s3.amazonaws.com/FaceSpace/" + s3path;
            }
            else
            {
                String dest = _localPath + "\\" + directory + _directorySeparator + Path.GetFileName(localFilename);
                System.IO.File.Copy(localFilename, dest, true);
                return _virtualDirectory + "\\" + directory + _directorySeparator + Path.GetFileName(localFilename);
            }
        }
示例#19
0
        private static void UploadFile(FileInfo fileInfo)
        {
            System.Console.WriteLine("Uploading " + fileInfo.Name);
            using (FileStream inputFileStream = new FileStream(fileInfo.FullName, FileMode.Open))
            using (MemoryStream outputMemoryStream = new MemoryStream())
            using (CryptoStream cryptoStream = new CryptoStream(outputMemoryStream, _aesEncryptor, CryptoStreamMode.Write))
            {
                int data;
                while ((data = inputFileStream.ReadByte()) != -1)
                {
                    cryptoStream.WriteByte((byte)data);
                }
                cryptoStream.FlushFinalBlock();

                PutObjectRequest createRequest = new PutObjectRequest();
                createRequest.WithMetaData("x-amz-meta-LWT", fileInfo.LastWriteTime.ToString("G"));
                createRequest.WithBucketName(BucketName);
                createRequest.WithKey(fileInfo.Name);
                createRequest.WithInputStream(outputMemoryStream);

                _amazonS3Client.PutObject(createRequest);
            }
        }
示例#20
0
		private void UploadToS3(string backupPath, PeriodicBackupSetup localBackupConfigs)
		{
			var awsRegion = RegionEndpoint.GetBySystemName(localBackupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1;

			using (var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, awsRegion))
			using (var fileStream = File.OpenRead(backupPath))
			{
				var key = Path.GetFileName(backupPath);
				var request = new PutObjectRequest();
				request.WithMetaData("Description", GetArchiveDescription());
				request.WithInputStream(fileStream);
				request.WithBucketName(localBackupConfigs.S3BucketName);
				request.WithKey(key);

				using (client.PutObject(request))
				{
					logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
											  Path.GetFileName(backupPath), localBackupConfigs.S3BucketName, key));
				}
			}
		}
示例#21
0
        public ActionResult Upload()
        {
            //Convert pdf to swf
            /*
            Process pc = new Process();
            String currentPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            //ProcessStartInfo psi = new ProcessStartInfo(currentPath + "/Flash/pdf2swf.exe", currentPath+"/Flash/testPDF.pdf" +" -o " +currentPath+"/Flash/testPDF.swf");
            //pc.StartInfo = psi;
            pc.StartInfo.FileName = currentPath + "\\Flash\\pdf2swf.exe";
            pc.StartInfo.Arguments = currentPath + "\\Flash\\testPDF2.pdf" + " -o " + currentPath + "\\Flash\\testPDF2.swf";
            pc.Start();
            pc.WaitForExit();
            pc.Close();
             * */

            //Upload pdf file to the server and database

            foreach (string upload in Request.Files)
            {
                if (!helperClass.HasFile(Request.Files[upload])) continue;
                string path = "https://s3-us-west-1.amazonaws.com/airpdfstorage/";
                string filename = Path.GetFileName(Request.Files[upload].FileName);
                DateTime nowTime = DateTime.Now;
                string user = User.Identity.Name.ToString();

                //store to app_data folder for thumbnail
                string path2 = AppDomain.CurrentDomain.BaseDirectory + "App_Data/";
                Request.Files[upload].SaveAs(Path.Combine(path2, filename));

                //upload to amazon s3
                string accessKey = "AKIAIO4BEQ2UGFAMHRFQ";
                string secretAccessKey = "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";
                string bucketName = "airpdfstorage";
                string keyName = filename;
                AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey);
                PutObjectRequest request = new PutObjectRequest();
                request.WithInputStream(Request.Files[upload].InputStream);
                request.CannedACL = S3CannedACL.PublicRead;
                request.WithBucketName(bucketName);
                request.WithKey(keyName);
                request.StorageClass = S3StorageClass.ReducedRedundancy; //set storage to reduced redundancy
                client.PutObject(request);
                client.Dispose();

                using (var conn = new SqlConnection(connect))
                {
                    var qry = "INSERT INTO PDFs (Title, Author, uploadTime,fileURL) VALUES (@Title, @Author, @uploadTime, @fileURL)";
                    var cmd = new SqlCommand(qry, conn);
                    cmd.Parameters.AddWithValue("@Title", filename);
                    cmd.Parameters.AddWithValue("@Author", user);
                    cmd.Parameters.AddWithValue("@uploadTime", nowTime);
                    cmd.Parameters.AddWithValue("@fileURL", Path.Combine(path, filename));
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }

            return View();
        }