public PutObject ( PutObjectRequest request ) : PutObjectResponse | ||
request | PutObjectRequest | Container for the necessary parameters to execute the PutObject service method. |
return | PutObjectResponse |
public static Boolean SendImageToS3(String key, Stream imageStream) { var success = false; using (var client = new AmazonS3Client(RegionEndpoint.USWest2)) { try { PutObjectRequest request = new PutObjectRequest() { InputStream = imageStream, BucketName = BucketName, Key = key }; client.PutObject(request); success = true; } catch (Exception ex) { // swallow everything for now. } } return success; }
private static S3File UploadImage(string key, Stream inputStream) { var s3Config = new AmazonS3Config() { ServiceURL = "http://" + _s3_bucket_region }; using (var cli = new AmazonS3Client( _s3_access_key, _s3_secret_access_key, s3Config)) { PutObjectRequest req = new PutObjectRequest() { BucketName = _s3_bucket_name, ContentType = "image/jpg", InputStream = inputStream, Key = key, CannedACL = S3CannedACL.PublicRead }; var response = cli.PutObject(req); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("s3: upload failed."); } else { return new S3File() { Key = key, Url = HttpUtility.HtmlEncode( String.Format("http://{0}.{1}/{2}", _s3_bucket_name, _s3_bucket_region, key)) }; } } }
static void Main() { // Connect to Amazon S3 service with authentication BasicAWSCredentials basicCredentials = new BasicAWSCredentials("AKIAIIYG27E27PLQ6EWQ", "hr9+5JrS95zA5U9C6OmNji+ZOTR+w3vIXbWr3/td"); AmazonS3Client s3Client = new AmazonS3Client(basicCredentials); // Display all S3 buckets ListBucketsResponse buckets = s3Client.ListBuckets(); foreach (var bucket in buckets.Buckets) { Console.WriteLine(bucket.BucketName); } // Display and download the files in the first S3 bucket string bucketName = buckets.Buckets[0].BucketName; Console.WriteLine("Objects in bucket '{0}':", bucketName); ListObjectsResponse objects = s3Client.ListObjects(new ListObjectsRequest() { BucketName = bucketName }); foreach (var s3Object in objects.S3Objects) { Console.WriteLine("\t{0} ({1})", s3Object.Key, s3Object.Size); if (s3Object.Size > 0) { // We have a file (not a directory) --> download it GetObjectResponse objData = s3Client.GetObject( new GetObjectRequest() { BucketName = bucketName, Key = s3Object.Key }); string s3FileName = new FileInfo(s3Object.Key).Name; SaveStreamToFile(objData.ResponseStream, s3FileName); } } // Create a new directory and upload a file in it string path = "uploads/new_folder_" + DateTime.Now.Ticks; string newFileName = "example.txt"; string fullFileName = path + "/" + newFileName; string fileContents = "This is an example file created through the Amazon S3 API."; s3Client.PutObject(new PutObjectRequest() { BucketName = bucketName, Key = fullFileName, ContentBody = fileContents} ); Console.WriteLine("Created a file in Amazon S3: {0}", fullFileName); // Share the uploaded file and get a download URL string uploadedFileUrl = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest() { BucketName = bucketName, Key = fullFileName, Expires = DateTime.Now.AddYears(5) }); Console.WriteLine("File download URL: {0}", uploadedFileUrl); System.Diagnostics.Process.Start(uploadedFileUrl); }
protected void Page_Load(object sender, EventArgs e) { if (upload.PostedFile != null && upload.PostedFile.ContentLength > 0 && !string.IsNullOrEmpty(Request["awsid"]) && !string.IsNullOrEmpty(Request["awssecret"]) && !string.IsNullOrEmpty(this.bucket.Text)) { var name = "s3readersample/" + ImageUploadHelper.Current.GenerateSafeImageName(upload.PostedFile.InputStream, upload.PostedFile.FileName); var client = new Amazon.S3.AmazonS3Client(Request["awsid"], Request["awssecret"], Amazon.RegionEndpoint.EUWest1); //For some reason we have to buffer the file in memory to prevent issues... Need to research further var ms = new MemoryStream(); upload.PostedFile.InputStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var request = new Amazon.S3.Model.PutObjectRequest() { BucketName = this.bucket.Text, Key = name, InputStream = ms, CannedACL = Amazon.S3.S3CannedACL.PublicRead }; var response = client.PutObject(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { result.Text = "Successfully uploaded " + name + "to bucket " + this.bucket.Text; } else { result.Text = response.HttpStatusCode.ToString(); } } }
protected void Page_Load(object sender, EventArgs e) { if (upload.PostedFile != null && upload.PostedFile.ContentLength > 0 && !string.IsNullOrEmpty(Request["awsid"]) && !string.IsNullOrEmpty(Request["awssecret"]) && !string.IsNullOrEmpty(this.bucket.Text)) { var name = "s3readersample/" + ImageUploadHelper.Current.GenerateSafeImageName(upload.PostedFile.InputStream, upload.PostedFile.FileName); var client = new Amazon.S3.AmazonS3Client(Request["awsid"], Request["awssecret"], Amazon.RegionEndpoint.EUWest1); //For some reason we have to buffer the file in memory to prevent issues... Need to research further var ms = new MemoryStream(); upload.PostedFile.InputStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var request = new Amazon.S3.Model.PutObjectRequest() { BucketName = this.bucket.Text, Key = name, InputStream = ms, CannedACL = Amazon.S3.S3CannedACL.PublicRead }; var response = client.PutObject(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { result.Text = "Successfully uploaded " + name + "to bucket " + this.bucket.Text; } else { result.Text = response.HttpStatusCode.ToString(); } } }
static void Main(string[] args) { try { var client = new AmazonS3Client(); PutObjectResponse putResponse = client.PutObject(new PutObjectRequest { BucketName = BUCKET_NAME, FilePath = TEST_FILE }); GetObjectResponse getResponse = client.GetObject(new GetObjectRequest { BucketName = BUCKET_NAME, Key = TEST_FILE }); getResponse.WriteResponseStreamToFile(@"c:\talk\" + TEST_FILE); var url = client.GetPreSignedURL(new GetPreSignedUrlRequest { BucketName = BUCKET_NAME, Key = TEST_FILE, Expires = DateTime.Now.AddHours(1) }); OpenURL(url); } catch(Exception e) { Console.WriteLine(e.Message); } }
public async System.Threading.Tasks.Task<IHttpActionResult> PostUpload(string folder, string filekey) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var root = HttpContext.Current.Server.MapPath("~/App_Data/Temp/FileUploads"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } var provider = new MultipartFormDataStreamProvider(root); try { var result = await Request.Content.ReadAsMultipartAsync(provider); } catch (Exception ex) { } try { string bucketName = "aws-yeon-test-fims-support"; var credentials = new StoredProfileAWSCredentials("s3"); IAmazonS3 client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.APNortheast2); foreach (var file in provider.FileData) { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2), FilePath = file.LocalFileName //ContentType = "text/plain" }; putRequest.Headers.ContentLength = 168059; PutObjectResponse response = client.PutObject(putRequest); } } catch (AmazonS3Exception amazonS3Exception) { return InternalServerError(amazonS3Exception); } catch (Exception ex) { return InternalServerError(ex); } finally { } return Ok(); }
public static string UploadImageBlobAmazonS3(string fileName, string blobName, List <KeyValuePair <string, string> > metadataTags) { string bucketName = ConfigurationManager.AppSettings["AWSMediaItemsContainerName"]; string accessKey = ConfigurationManager.AppSettings["AWSAccessKey"]; string accessSecret = ConfigurationManager.AppSettings["AWSAccessSecret"]; using (var client = new Amazon.S3.AmazonS3Client(accessKey, accessSecret, Amazon.RegionEndpoint.APSoutheast2)) { //upload: http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpNET.html try { PutObjectRequest putRequest1 = new PutObjectRequest { BucketName = bucketName, Key = "images/" + blobName, FilePath = fileName, CannedACL = S3CannedACL.PublicRead }; PutObjectResponse response1 = client.PutObject(putRequest1); return("https://s3-ap-southeast-2.amazonaws.com/openchargemap/images/" + blobName); /* // 2. Put object-set ContentType and add metadata. * PutObjectRequest putRequest2 = new PutObjectRequest * { * BucketName = bucketName, * Key = keyName, * FilePath = filePath, * ContentType = "text/plain" * }; * putRequest2.Metadata.Add("x-amz-meta-title", "someTitle"); * * PutObjectResponse response2 = client.PutObject(putRequest2);*/ } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "For service sign up go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when writing an object" , amazonS3Exception.Message); } return(null); } } }
public static void CreateTestBase() { Client = new AmazonS3Client(); bucketName = S3TestUtils.CreateBucket(Client); Client.PutObject(new PutObjectRequest { BucketName = bucketName }); }
public Uri Convert(byte[] bytes) { string url; using (IAmazonS3 client = new AmazonS3Client(AccessKey, SecretKey, RegionEndpoint.USEast1)) { Guid imageFilename = Guid.NewGuid(); PutObjectRequest request = BuildRequest(imageFilename, new MemoryStream(bytes)); client.PutObject(request); url = string.Format("https://s3.amazonaws.com/{0}/{1}/{2}", BucketName, FolderName, imageFilename); } return new Uri(url); }
private void EditFile(HttpChallenge httpChallenge, bool delete, TextWriter msg) { var filePath = httpChallenge.FilePath; // We need to strip off any leading '/' in the path or // else it creates a path with an empty leading segment if (filePath.StartsWith("/")) { filePath = filePath.Substring(1); } using (var s3 = new Amazon.S3.AmazonS3Client( CommonParams.ResolveCredentials(), CommonParams.RegionEndpoint)) { if (delete) { LOG.Debug("Deleting S3 object at Bucket [{0}] and Key [{1}]", BucketName, filePath); var s3Requ = new Amazon.S3.Model.DeleteObjectRequest { BucketName = BucketName, Key = filePath, }; var s3Resp = s3.DeleteObject(s3Requ); if (LOG.IsDebugEnabled) { LOG.Debug("Delete response: [{0}]", NLog.Targets.DefaultJsonSerializer.Instance.SerializeObject(s3Resp)); } msg.WriteLine("* Challenge Response has been deleted from S3"); msg.WriteLine(" at Bucket/Key: [{0}/{1}]", BucketName, filePath); } else { var s3Requ = new Amazon.S3.Model.PutObjectRequest { BucketName = BucketName, Key = filePath, ContentBody = httpChallenge.FileContent, ContentType = ContentType, CannedACL = S3CannedAcl, }; var s3Resp = s3.PutObject(s3Requ); msg.WriteLine("* Challenge Response has been written to S3"); msg.WriteLine(" at Bucket/Key: [{0}/{1}]", BucketName, filePath); msg.WriteLine("* Challenge Response should be accessible with a MIME type of [text/json]"); msg.WriteLine(" at: [{0}]", httpChallenge.FileUrl); } } }
public Uri Save(string base64ImageString) { string url; using (IAmazonS3 client = new AmazonS3Client(AccessKey, SecretKey, RegionEndpoint.USEast1)) { MemoryStream ms = GetMemoryStreamFromBase64(base64ImageString); Guid imageFilename = Guid.NewGuid(); PutObjectRequest request = BuildRequest(imageFilename, ms); client.PutObject(request); url = "https://s3.amazonaws.com/FireTower_DisasterImages/disasters/" + imageFilename.ToString(); } return new Uri(url); }
private static string PutObj(AmazonS3Client client) { string time = DateTime.Now.ToString("hhmmsstt"); // Create a PutObject request PutObjectRequest putObjRequest = new PutObjectRequest { BucketName = "com.loofah.photos", Key = time, FilePath = "C:\\Users\\Ryan\\Pictures\\cz1jb.jpg" }; // Put object PutObjectResponse putObjResponse = client.PutObject(putObjRequest); return time; }
public void CreateVersion(Version version) { if (version == null) throw new ArgumentNullException("version", "Version cannot be null."); if (version.AppKey == Guid.Empty) throw new ArgumentException("App key cannot be empty.", "version.AppKey"); try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var appsController = new Apps(Context); var app = appsController.GetApp(version.AppKey); version.GroupKey = app.GroupKey; var indexesController = new Internal.Indexes(Context); using (var stream = version.Serialise()) { string indexPath = GetAppVersionsIndexPath(version.AppKey); var index = indexesController.LoadIndex(indexPath); if (index.Entries.Any(e => e.Key == version.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_VERSIONS_CONTAINER_PATH, version.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } index.Entries.Add(new Internal.EntityIndexEntry() { Key = version.Key, Name = CreateVersionIndexName(version) }); Internal.Indexes.NameSortIndex(index, true); indexesController.UpdateIndex(indexPath, index); } } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating version.", awsEx); } catch (Exception ex) { throw new DeploymentException("Failed creating version.", ex); } }
public void CreateS3Bucket(string bucketName, string key, Credentials credentials, AmazonS3Config config) { var s3Client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, credentials.SessionToken, config); string content = "Hello World2!"; // Put an object in the user's "folder". s3Client.PutObject(new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = content }); Console.WriteLine("Updated key={0} with content={1}", key, content); }
public void CreateInstance(Instance instance) { if (instance == null) throw new ArgumentNullException("instance", "Instance cannot be null."); if (instance.TargetKey == Guid.Empty) throw new ArgumentException("Target key cannot be empty.", "instance.TargetKey"); using (var stream = instance.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var targetsController = new Targets(Context); if (!targetsController.TargetExists(instance.TargetKey)) throw new TargetNotFoundException(String.Format("Target with the key \"{0}\" could not be found.", instance.TargetKey)); var indexesController = new Internal.Indexes(Context); string indexPath = GetTargetInstancesIndexPath(instance.TargetKey); var instanceIndex = indexesController.LoadIndex(indexPath); if (instanceIndex.Entries.Any(e => e.Key == instance.Key)) { throw new DeploymentException("Target instances index already contains entry for new instance key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_INSTANCES_CONTAINER_PATH, instance.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } instanceIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = instance.Key, Name = instance.Name }); Internal.Indexes.NameSortIndex(instanceIndex); indexesController.UpdateIndex(indexPath, instanceIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating instance.", awsEx); } } }
public void CreateTarget(Target target) { if (target == null) throw new ArgumentNullException("target", "Target cannot be null."); if (target.GroupKey == Guid.Empty) throw new ArgumentException("Group key cannot be empty.", "target.GroupKey"); using (var stream = target.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var groupsController = new Groups(Context); if (!groupsController.GroupExists(target.GroupKey)) throw new GroupNotFoundException(String.Format("Group with the key \"{0}\" could not be found.", target.GroupKey)); var indexesController = new Internal.Indexes(Context); string indexPath = GetGroupTargetsIndexPath(target.GroupKey); var appIndex = indexesController.LoadIndex(indexPath); if (appIndex.Entries.Any(e => e.Key == target.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_TARGETS_CONTAINER_PATH, target.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } appIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = target.Key, Name = target.Name }); Internal.Indexes.NameSortIndex(appIndex); indexesController.UpdateIndex(indexPath, appIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating target.", awsEx); } } }
public string UploadContentFile(string uniqueName, string fileExtension, Stream fileStream, bool makePrivate = false) { // file extension has dot at start. string fileName = string.Format("{0}.{1}", uniqueName, fileExtension); IAmazonS3 client = null; using (client = new AmazonS3Client(_awsAccessKeyId, _awsSecretAccessKey)) { fileStream.Position = 0; try { // simple object put PutObjectRequest request = new PutObjectRequest() { //ContentBody = "You are such a brilliant person reading open documents.", BucketName = _awsBucketName, Key = string.Format("{0}/{1}", _awsContentFilePath, fileName), CannedACL = S3CannedACL.PublicRead, InputStream = fileStream }; PutObjectResponse response = client.PutObject(request); string url = string.Format("https://{0}.s3-{1}.amazonaws.com/{2}/{3}", _awsBucketName, _awsRegion, _awsContentFilePath, fileName ); return url; } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Please check the provided AWS Credentials."); throw new Exception("Storage access denied."); } else { Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message); throw new Exception("Write failed."); } } } }
public virtual void AddImage(AmazonDynamoDBClient dynamoDbClient, string tableName, AmazonS3Client s3Client, string bucketName, string imageKey, string filePath) { try { if (File.Exists(filePath)) { // Create the upload request var putObjectRequest = new PutObjectRequest { BucketName = bucketName, Key = imageKey, FilePath = filePath }; // Upload the object s3Client.PutObject(putObjectRequest); // Create the put item request to submit to DynamoDB var putItemRequest = new PutItemRequest { TableName = tableName, Item = new Dictionary<string, AttributeValue> { {"Key", new AttributeValue {S = imageKey}}, {"Bucket", new AttributeValue {S = bucketName}} } }; dynamoDbClient.PutItem(putItemRequest); _Default.LogMessageToPage("Added imageKey: {0}", imageKey); } else { _Default.LogMessageToPage("Skipped imageKey: {0}", imageKey); } } catch (Exception ex) { _Default.LogMessageToPage("AddImage Error: {0}", ex.Message); } }
public ActionResult Create(Photo photo) { var photoFile = Request.Files["photo-file"]; photo.User = Session["user"] as string; photo.PhotoUrl = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(photoFile.FileName); using (var s3 = new AmazonS3Client()) { s3.PutObject(new Amazon.S3.Model.PutObjectRequest { CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead, BucketName = "betamore-photoup", InputStream = photoFile.InputStream, Key = photo.PhotoUrl }); } Photo.Create(photo); return RedirectToAction("Index"); }
public bool StoreFile() { var s3Client = new AmazonS3Client("AKIAJMOTY7XI2PXO4NWA", "1RXLiAR42GzV8yuEC70MkcBQzpfDDvGh+7Mx3+/a"); s3Client. //Saving File to local disk folder. string filePath = Server.MapPath("S3FilesUpload") + "\\" + fileUpload.FileName; string fileExtension = fileUpload.FileName.Substring (fileUpload.FileName.LastIndexOf(".") + 1); fileUpload.SaveAs(filePath); string contentType = GetContentType(fileExtension); //Push the given object into S3 Bucket var objReq = new PutObjectRequest { Key = fileUpload.FileName, FilePath = filePath, ContentType = contentType, BucketName = "YourBucketName", CannedACL = S3CannedACL.Private, }; PutObjectResponse response = s3Client.PutObject(objReq); if (response.ETag != null) { string etag = response.ETag; string versionID = response.VersionId; } //Deleting Locally Saved File if (File.Exists(filePath)) { File.Delete(filePath); } return true; }
private void EditFile(HttpChallenge httpChallenge, bool delete) { var filePath = httpChallenge.FilePath; // We need to strip off any leading '/' in the path or // else it creates a path with an empty leading segment if (filePath.StartsWith("/")) { filePath = filePath.Substring(1); } using (var s3 = new Amazon.S3.AmazonS3Client( CommonParams.ResolveCredentials(), CommonParams.RegionEndpoint)) { if (delete) { var s3Requ = new Amazon.S3.Model.DeleteObjectRequest { BucketName = BucketName, Key = filePath, }; var s3Resp = s3.DeleteObject(s3Requ); } else { var s3Requ = new Amazon.S3.Model.PutObjectRequest { BucketName = BucketName, Key = filePath, ContentBody = httpChallenge.FileContent, ContentType = ContentType, CannedACL = S3CannedAcl, }; var s3Resp = s3.PutObject(s3Requ); } } }
public void UploadProfilePicture(string a_userId, Stream a_data) { IAmazonS3 client; using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { Console.WriteLine("Uploading an object"); try { PutObjectRequest putRequest = new PutObjectRequest { BucketName = Options.Bucket, Key = a_userId, InputStream = a_data, CannedACL = S3CannedACL.PublicRead }; PutObjectResponse response = client.PutObject(putRequest); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "For service sign up go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when writing an object" , amazonS3Exception.Message); } } } }
// Sets up the student's input bucket with sample data files retrieved from the lab bucket. public static void Setup(AmazonS3Client s3ForStudentBuckets) { RegionEndpoint region = RegionEndpoint.USWest2; AmazonS3Client s3ForLabBucket; string textContent = null; bool exist = false; s3ForLabBucket = new AmazonS3Client(region); DataTransformer.CreateBucket(DataTransformer.InputBucketName); for (int i = 0; i < labBucketDataFileKeys.Length; i++) { GetObjectRequest requestForStream = new GetObjectRequest { BucketName = labS3BucketName, Key = labBucketDataFileKeys[i] }; using (GetObjectResponse responseForStream = s3ForLabBucket.GetObject(requestForStream)) { using (StreamReader reader = new StreamReader(responseForStream.ResponseStream)) { textContent = reader.ReadToEnd(); PutObjectRequest putRequest = new PutObjectRequest { BucketName = DataTransformer.InputBucketName, Key = labBucketDataFileKeys[i].ToString().Split('/').Last(), ContentBody = textContent }; putRequest.Metadata.Add("ContentLength", responseForStream.ContentLength.ToString()); s3ForStudentBuckets.PutObject(putRequest); } } } }
public static void UploadFileToAmazonS3(FileStream fu, string bucketName, string fileName) { if (fu != null) { if (FileExistsInAmazonS3(System.Configuration.ConfigurationSettings.AppSettings["AWSBucket_Small"], fileName)) { DeleteFileInAmazonS3(System.Configuration.ConfigurationSettings.AppSettings["AWSBucket_Small"], fileName); } Amazon.S3.AmazonS3Client s3Client = new Amazon.S3.AmazonS3Client(); PutObjectRequest request = new PutObjectRequest(); request.InputStream = fu; request.BucketName = bucketName; request.CannedACL = S3CannedACL.PublicRead; request.Key = fileName; s3Client.PutObject(request); } else { throw new System.Exception("File Empty."); } }
private static void UploadFile(aws_class_data aws_data) { using (var client = new Amazon.S3.AmazonS3Client(aws_data.AwsAccessKey, aws_data.AwsSecretKey, Amazon.RegionEndpoint.EUCentral1)) { var fs = new FileStream(aws_data.FileToUpload, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { var request = new PutObjectRequest(); request.BucketName = aws_data.AwsS3BucketName + "/" + aws_data.AwsS3FolderName; request.CannedACL = S3CannedACL.Private; request.Key = Path.GetFileName(aws_data.FileToUpload); request.InputStream = fs; client.PutObject(request); } catch (AmazonS3Exception ex) { ErrorLog.ErrorLog.toErrorFile(ex.GetBaseException().ToString()); if (string.IsNullOrEmpty(ex.ErrorCode)) { ErrorLog.ErrorLog.toErrorFile("Amazon error code: {0}" + "None"); } else { ErrorLog.ErrorLog.toErrorFile("Amazon error code: {0}" + ex.ErrorCode); } //Console.WriteLine("Amazon error code: {0}", string.IsNullOrEmpty(ex.ErrorCode) ? "None" : ex.ErrorCode); ErrorLog.ErrorLog.toErrorFile("Exception message: {0}" + ex.Message); } catch (Exception ex) { ErrorLog.ErrorLog.toErrorFile(ex.GetBaseException().ToString()); ErrorLog.ErrorLog.toErrorFile("Exception message: {0}" + ex.Message); } } }
public void CreateGroup(Group group) { if (group == null) throw new ArgumentNullException("group", "Group cannot be null."); using (var stream = group.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var indexesController = new Internal.Indexes(Context); var index = indexesController.LoadIndex(STR_GROUP_INDEX_PATH); if (index.Entries.Any(e => e.Key == group.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_GROUPS_CONTAINER_PATH, group.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } index.Entries.Add(new Internal.EntityIndexEntry() { Key = group.Key, Name = group.Name }); Internal.Indexes.NameSortIndex(index); indexesController.UpdateIndex(STR_GROUP_INDEX_PATH, index); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating group.", awsEx); } } }
public virtual void PutObject(AmazonS3Client s3Client, string bucketName, string sourceFile, string objectKey) { // Create the request var putObjectRequest = new PutObjectRequest { BucketName = bucketName, Key = objectKey, FilePath = sourceFile }; // Upload the object s3Client.PutObject(putObjectRequest); }
public void UpdateInstance(Instance updatedInstance) { if (updatedInstance == null) throw new ArgumentNullException("updatedInstance", "Instance cannot be null."); var existingInstance = GetInstance(updatedInstance.Key); // Don't allow moving between targets. updatedInstance.TargetKey = existingInstance.TargetKey; using (var stream = updatedInstance.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_INSTANCES_CONTAINER_PATH, updatedInstance.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } var indexesController = new Internal.Indexes(Context); string indexPath = GetTargetInstancesIndexPath(updatedInstance.TargetKey); indexesController.PutIndexEntry(indexPath, new Internal.EntityIndexEntry() { Key = updatedInstance.Key, Name = updatedInstance.Name }); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed updating instance.", awsEx); } } }
private void EditFile(HttpChallenge httpChallenge, bool delete) { var filePath = httpChallenge.FilePath; // We need to strip off any leading '/' in the path or // else it creates a path with an empty leading segment if (filePath.StartsWith("/")) filePath = filePath.Substring(1); using (var s3 = new Amazon.S3.AmazonS3Client( CommonParams.ResolveCredentials(), CommonParams.RegionEndpoint)) { if (delete) { var s3Requ = new Amazon.S3.Model.DeleteObjectRequest { BucketName = BucketName, Key = filePath, }; var s3Resp = s3.DeleteObject(s3Requ); } else { var s3Requ = new Amazon.S3.Model.PutObjectRequest { BucketName = BucketName, Key = filePath, ContentBody = httpChallenge.FileContent, ContentType = ContentType, CannedACL = S3CannedAcl, }; var s3Resp = s3.PutObject(s3Requ); } } }
//Put 'em in the cloud static void StrayMapperImageUploader() { try { System.IO.DirectoryInfo localStrayMapperImageDirectory = new DirectoryInfo(Credentials.imageDir); foreach (FileInfo imageFile in localStrayMapperImageDirectory.GetFiles()) { var request = new PutObjectRequest() .WithBucketName(Credentials.bucketName) .WithCannedACL(S3CannedACL.PublicRead) .WithFilePath(imageFile.FullName) .WithKey("images/" + imageFile.Name); //the key will need to be modified to the key/faux-directory you're keeping images in on your hosting client AmazonS3Client strayMapper = new AmazonS3Client(Credentials.accessKey, Credentials.secretAccessKey); using (var upload = strayMapper.PutObject(request)) { } } } catch (Exception error) { SendMeExceptionError(error); } }
// /api/v1/user/<username>/media-objects // GET: Returns a list of media objects for the user. /*public List<Media> Get(String username, String modifier) { if (modifier.Equals("media-objects")) { using (var db = new MemoryDB()) { var dbUser = (from us in db.User where us.username == username select us).SingleOrDefault(); if (dbUser == null) { var responseMsg = new HttpResponseMessage { Content = new StringContent("There was no user found.") }; throw new HttpResponseException(responseMsg); } var dbMedia = (from me in db.Media where me.user.id == dbUser.id select me).ToList(); if (dbMedia == null) { var responseMsg = new HttpResponseMessage { Content = new StringContent("There were no media files found.") }; throw new HttpResponseException(responseMsg); } else { return dbMedia; } } } else { var responseMsg = new HttpResponseMessage { Content = new StringContent("The modifier was not correct.") }; throw new HttpResponseException(responseMsg); } }*/ // /api/v1/user/<username>/<modifier> (sounds, videos, pictures) // POST: Create new media file. In body must be either "sound-file", "video-file" or "picture-file". public IHttpActionResult Post(String id, String modifier,[FromBody]String id2) { string username = id; string friendsname = id2; string bucketName = "memorybucket"; string awsAccessKeyId = ""; string awsSecretAccessKey = ""; string URLforFile = "http://memoryapi-dev.elasticbeanstalk.com/"; var DB = new MemoryDB(); User userObj = DB.User.FirstOrDefault(x => x.username == username); if(modifier.Equals("pictures")) { var uploadFile = HttpContext.Current.Request.Files["picture-file"]; if ((uploadFile != null) && (uploadFile.ContentLength > 0)) { byte[] dataArr = new byte[uploadFile.ContentLength]; uploadFile.InputStream.Read(dataArr, 0, uploadFile.ContentLength); Stream stream = new MemoryStream(dataArr); var fileName = Convert.ToString(DateTime.Now.ToFileTime()); string file = uploadFile.FileName; string fileType = file.Substring(file.IndexOf('.') + 1); var pictureObj = new PictureMedia { fileUrl = URLforFile + fileName + "." + fileType, container = uploadFile.ContentType, width = Convert.ToInt32(HttpContext.Current.Request.QueryString["width"]), height = Convert.ToInt32(HttpContext.Current.Request.QueryString["height"]) }; try { using (var ac = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, Amazon.RegionEndpoint.EUCentral1)) { ac.PutObject(new PutObjectRequest() { InputStream = stream, BucketName = bucketName, Key = fileName + "." + fileType }); } } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } using (var db = new MemoryDB()) { try { userObj.mediaList.Add(pictureObj); db.SaveChanges(); return Ok("The picture file has been uploaded."); } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } } } } if (modifier.Equals("sounds")) { var uploadFile = HttpContext.Current.Request.Files["sound-file"]; if ((uploadFile != null) && (uploadFile.ContentLength > 0)) { byte[] dataArr = new byte[uploadFile.ContentLength]; uploadFile.InputStream.Read(dataArr, 0, uploadFile.ContentLength); Stream stream = new MemoryStream(dataArr); var fileName = Convert.ToString(DateTime.Now.ToFileTime()); string file = uploadFile.FileName; string fileType = file.Substring(file.IndexOf('.') + 1); var soundObj = new SoundMedia { fileUrl = URLforFile + fileName + "." + fileType, container = uploadFile.ContentType, duration = Convert.ToInt32(HttpContext.Current.Request.QueryString["duration"]), codec = Convert.ToString(HttpContext.Current.Request.QueryString["codec"]), bitRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["bitrate"]), channels = Convert.ToInt32(HttpContext.Current.Request.QueryString["channels"]), samplingRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["samplingrate"]) }; try { using (var ac = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, Amazon.RegionEndpoint.EUCentral1)) { ac.PutObject(new PutObjectRequest() { InputStream = stream, BucketName = bucketName, Key = fileName + "." + fileType }); } } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } using (var db = new MemoryDB()) { try { userObj.mediaList.Add(soundObj); db.SaveChanges(); return Ok("The sound file has been uploaded."); } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } } } } if (modifier.Equals("videos")) { var uploadFile = HttpContext.Current.Request.Files["video-file"]; if ((uploadFile != null) && (uploadFile.ContentLength > 0)) { byte[] dataArr = new byte[uploadFile.ContentLength]; uploadFile.InputStream.Read(dataArr, 0, uploadFile.ContentLength); Stream stream = new MemoryStream(dataArr); var fileName = Convert.ToString(DateTime.Now.ToFileTime()); string file = uploadFile.FileName; string fileType = file.Substring(file.IndexOf('.'), +1); var videoObj = new VideoMedia { fileUrl = URLforFile + fileName + "." + fileType, container = uploadFile.ContentType, width = Convert.ToInt32(HttpContext.Current.Request.QueryString["width"]), height = Convert.ToInt32(HttpContext.Current.Request.QueryString["height"]), videoCodec = Convert.ToString(HttpContext.Current.Request.QueryString["videocodec"]), videoBitRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["videobitrate"]), frameRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["framerate"]), audioCodec = Convert.ToString(HttpContext.Current.Request.QueryString["audiocodec"]), audioBitRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["audiobitrate"]), samplingRate = Convert.ToInt32(HttpContext.Current.Request.QueryString["samplingRate"]) }; try { using (var ac = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, Amazon.RegionEndpoint.EUCentral1)) { ac.PutObject(new PutObjectRequest() { InputStream = stream, BucketName = bucketName, Key = fileName + "." + fileType }); } } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } using (var db = new MemoryDB()) { try { userObj.mediaList.Add(videoObj); db.SaveChanges(); return Ok("The video file has been uploaded."); } catch (Exception e) { var errorResponseMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(errorResponseMsg); } } } if (modifier.Equals("friends")) { using (var db = new MemoryDB()) { var dbUser = (from us in db.User.Include("friendList") where us.username == username select us).SingleOrDefault(); var dbFriend = (from us in db.User where us.username == friendsname select us).SingleOrDefault(); if ((dbUser != null) && (dbFriend != null)) { try { dbUser.friendList.Add(dbFriend); db.SaveChanges(); return Ok("Friend was successfully added."); } catch (Exception e) { var responseErrorMsg = new HttpResponseMessage { Content = new StringContent(string.Format("This is wrong: {0}", e)) }; throw new HttpResponseException(responseErrorMsg); } } else { var responseMsg = new HttpResponseMessage { Content = new StringContent("User not found or friend not found.") }; throw new HttpResponseException(responseMsg); } } } else { var responseMsg = new HttpResponseMessage { Content = new StringContent("You did not use the correct modifier.") }; throw new HttpResponseException(responseMsg); } } var lastErrorMsg = new HttpResponseMessage { Content = new StringContent(string.Format("The modifier was wrong, please state the correct one!")) }; throw new HttpResponseException(lastErrorMsg); }
public static void WriteToBucket(string bucketName, string key, string content) { using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret)) { var request = new PutObjectRequest() { ContentBody = content, BucketName = bucketName, Key = key }; client.PutObject(request); } }
public static void WriteFileToBucket(string bucketName, string key, string filename) { using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret)) { var request = new PutObjectRequest() { FilePath = filename, BucketName = bucketName, Key = key }; client.PutObject(request); } }
// methods public void Run(Object obj) { // counter for retires if (retry > 0) { // make the amazon client AmazonS3Client s3Client = new AmazonS3Client(awsKey, awsSecret, RegionEndpoint.GetBySystemName(region)); try { // request object with file PutObjectRequest req = new PutObjectRequest(); req.BucketName = bucket; req.Key = key; req.FilePath = file; req.CannedACL = ACL(); req.Timeout = new TimeSpan(2, 0, 0); // set the appropriate mime type if (file.Contains(".")) { string ext = file.Substring(file.LastIndexOf('.')); string mime = AmazonS3Util.MimeTypeFromExtension(ext); req.Headers.ContentType = mime; } // set timeout if(timeout > 0) { req.Timeout = new TimeSpan(0, 0, timeout); } // put the object PutObjectResponse res = s3Client.PutObject(req); // feedback Console.WriteLine("File '" + key + "' in bucket '" + bucket + "' has been added"); } catch (Exception e) { // output error Console.WriteLine("Warning adding file '" + file + "' in bucket '" + bucket + "': " + e.Message + " (operation will be retried)"); // lower the counter and rerun retry = retry - 1; Run(obj); } } else { // exceeded retries, time to output error Console.WriteLine("Error adding file '" + file + "' in bucket '" + bucket + "'"); } }
public void UpdateIndex(string path, EntityIndex index) { var serialised = SerialiseIndex(index.Entries); var stream = new MemoryStream(serialised.Length); var writer = new StreamWriter(stream); writer.Write(serialised); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { // Check that the index has not been updated before pushing the change - reduce the risks. try { using (var objectMetaDataResponse = client.GetObjectMetadata(new GetObjectMetadataRequest() { BucketName = Context.BucketName, Key = path, ETagToMatch = index.ETag })) { } } catch (AmazonS3Exception awsEx) { if (awsEx.StatusCode != System.Net.HttpStatusCode.NotFound) throw; } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = path, GenerateMD5Digest = true, InputStream = stream })) { } } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed loading group index", awsEx); } }