Пример #1
0
        private void UploadToAmazon(byte[] pictureBinary, string fileName)
        {
            try
            {
                //foldername = Path.Combine(_webHelper.MapPath("~/content/images/"), foldername);

                //NameValueCollection appSettings = ConfigurationManager.AppSettings;
                string          awsAccessKeyId     = _config.AmazonS3AccessKey;
                string          awsSecretAccessKey = _config.AmazonS3SecretKey;
                TransferUtility transferUtility    = new TransferUtility(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.APSoutheast1);

                // DirectoryInfo directoryInfo = new DirectoryInfo(foldername);
                TransferUtilityUploadRequest transferUtilityUploadRequest = new TransferUtilityUploadRequest();
                transferUtilityUploadRequest.BucketName = "image.eldago.com";
                transferUtilityUploadRequest.CannedACL  = S3CannedACL.PublicRead;
                //transferUtilityUploadRequest.Timeout = 360000000;
                transferUtilityUploadRequest.PartSize        = 2097152L;
                transferUtilityUploadRequest.AutoCloseStream = true;
                Stream stream = new MemoryStream(pictureBinary);
                //string key = fileInfo.FullName.ToLower().Replace(_config.TempImageFolder.Trim().ToLower(), "").Replace("\\", "/");
                transferUtilityUploadRequest.Key         = fileName;
                transferUtilityUploadRequest.InputStream = stream;
                //System.Threading.Tasks.Task oTask = transferUtility.UploadAsync(transferUtilityUploadRequest);
                AsyncCallback callback    = new AsyncCallback(this.uploadComplete);
                IAsyncResult  asyncResult = transferUtility.BeginUpload(transferUtilityUploadRequest, callback, fileName);
                transferUtility.EndUpload(asyncResult);
            }
            catch (Exception ex)
            {
            }
        }
Пример #2
0
        private void uploadData(String filePath)
        {
            String bucketName = "CHANGE ME";
            String accessKey  = "CHANGE ME";
            String secretKey  = "CHANGE ME";

            AmazonS3Client amazonClient = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast2);

            try{
                // Step 1 : Create "Transfer Utility" (replacement of old "Transfer Manager")
                TransferUtility fileTransferUtility =
                    new TransferUtility(amazonClient);


                // Step 2 : Create Request object
                TransferUtilityUploadRequest uploadRequest =
                    new TransferUtilityUploadRequest
                {
                    BucketName = bucketName + "/" + userName_,
                    FilePath   = filePath,
                    //Key = secretKey,
                    //CannedACL = S3CannedACL.PublicRead
                };


                // Step 3 : Event Handler that will be automatically called on each transferred byte
                uploadRequest.UploadProgressEvent +=
                    new EventHandler <UploadProgressArgs>
                        (uploadRequest_UploadPartProgressEvent);

                // Step 4 : Hit upload and send data to S3
                //fileTransferUtility.Upload(uploadRequest);
                fileTransferUtility.BeginUpload(uploadRequest, new AsyncCallback(uploadComplete), null);
            }

            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message, s3Exception.InnerException);
            }
        }
Пример #3
0
        public void Upload()
        {
            // Loop through each file in the request
            for (int i = 0; i < HttpContext.Request.Files.Count; i++)
            {
                // Pointer to file
                var file = HttpContext.Request.Files[i];
                var filename = User.Identity.Name + "_" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString().Replace('.', '_') + "_" + file.FileName;
                int width = 0, height = 0;
                try
                {
                    using (var client = new TransferUtility(AuthConfig.AWSPUBLIC, AuthConfig.AWSPRIVATE))
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            using (var yourBitmap = new Bitmap(file.InputStream))
                            {
                                yourBitmap.Save(memoryStream, ImageFormat.Jpeg);

                                width  = yourBitmap.Width;
                                height = yourBitmap.Height;

                                AsyncCallback callback = new AsyncCallback(uploadComplete);
                                var           request  = new TransferUtilityUploadRequest();
                                request.BucketName = "TTPosts";
                                //create a hash of the user, the current time, and the file name
                                //to avoid collisions
                                request.Key         = filename;
                                request.InputStream = memoryStream;
                                //makes public
                                request.AddHeader("x-amz-acl", "public-read");
                                IAsyncResult ar = client.BeginUpload(request, callback, null);
                                client.EndUpload(ar);
                            }
                        }
                    }
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                         amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                        Console.WriteLine("Please check the provided AWS Credentials.");
                        Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                    }
                    else
                    {
                        Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
                    }
                    return;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                Posts p = new Posts()
                {
                    title = "$" + HttpContext.Request.Form["tags"], filename = "http://s3.amazonaws.com/TTPosts/" + filename, owner = WebSecurity.GetUserId(User.Identity.Name), dateuploaded = DateTime.UtcNow, width = width, height = height
                };

                new TTRESTService().PostPost(p);
            }
        }