private static bool UploadFileToS3(string userId, string filePath, out string keyName) { var rsp = S3Helper.Instance().UploadFileToS3(GetBucketName(), filePath, userId); keyName = rsp.Result; return(true); }
private async void uploadToolStripMenuItem_Click(object sender, EventArgs e) { try { var s3BucketName = GetS3BucketName(_currentNode); S3Helper s3Helper = new S3Helper( GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color, s3BucketName ); DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { string filePath = openFileDialog1.FileName; var s3FilePath = _currentNode.FullPath.Substring(_currentNode.FullPath.IndexOf("\\") + 1) + "/" + Path.GetFileName(openFileDialog1.FileName); var stream = new FileStream(filePath, FileMode.Open); await s3Helper.UploadFile(s3FilePath, stream); RefreshGridviewControl(); WriteNotification(string.Format("File {0} was uploaded to {1}", Path.GetFileName(s3FilePath), s3BucketName)); } } catch (Exception ex) { HandleException(ex); } }
public void SetAcl_Should_Succeed() { // Get the client details from the stored client details (rather than embed secret keys in the test). // Ensure that your AWS/Secret keys have been stored before running. var store = new ClientDetailsStore(); AwsClientDetails clientDetails = store.Load(Container); S3Helper helper = new S3Helper(clientDetails); const string bucketName = "ExampleTestBucket"; const string key = "ExampleObject"; // Put a simple text object into the bucket to delete. helper.CreateBucket(bucketName); helper.PutTextObject(bucketName, key, "Example text to store in the object"); try { helper.SetAcl(bucketName, "AuthenticatedRead", key); } finally { helper.DeleteObject(bucketName, key); helper.DeleteBucket(bucketName); } }
/// <summary> /// Make container/directory (depending on platform). /// assumption being last part of url is the new container. /// With S3 "containers" could really be the bucket for the account /// /// IMPORTANT NOTE: /// /// For S3 the bucket comes from the url. /// The container name is just the fake virtual directory. /// ie blob of 0 bytes. /// </summary> /// <param name="container"></param> public void MakeContainer(string containerName) { using (IAmazonS3 client = S3Helper.GenerateS3Client(ConfigHelper.AWSAccessKeyID, ConfigHelper.AWSSecretAccessKeyID)) { client.PutBucket(containerName); } }
// // GET: /Manage/Index public async Task <ActionResult> Index(ManageMessageId?message) { ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." : message == ManageMessageId.Error ? "An error has occurred." : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." : ""; var userId = User.Identity.GetUserId(); var model = new IndexViewModel { HasPassword = HasPassword(), PhoneNumber = await UserManager.GetPhoneNumberAsync(userId), TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId), Logins = await UserManager.GetLoginsAsync(userId), BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId) }; var key = userId; var profilePic = S3Helper.GetFileObject(key); ViewBag.ProfilePicUrl = !string.IsNullOrEmpty(profilePic) ? profilePic: "~/Content/img/user.png"; //imgProf;//"~/Content/img/user.jpg"; ViewData["ProfilePicUrl"] = profilePic; return(View(model)); }
public Function() { _SM = new SMHelper(); _S3 = new S3Helper(); _messages = new List <Message>(); _callbacks = new List <CallbackQuery>(); }
private static void InitializeS3() { var bucketName = DynamoDBHelper.Instance().FetchMetadata(Constants.TOP_LEVEL_BUCKET_NAME_METADATA_KEY); if (string.IsNullOrEmpty(bucketName)) // The bucket doesnt exist yet, create it now { bool bucketCreated = false; do { var hashValue = Math.Abs(DateTime.Now.ToString().GetHashCode()); var newBucketName = Constants.APPNAME + hashValue.ToString(); newBucketName = newBucketName.ToLower(); bucketCreated = S3Helper.Instance().CreateS3Bucket(newBucketName); if (bucketCreated) { UpdateMetadataWithS3BucketName(newBucketName); } var thumbnailBucketName = newBucketName + "-thumbnails"; bucketCreated = S3Helper.Instance().CreateS3Bucket(thumbnailBucketName); if (bucketCreated) { UpdateMetadataWithS3ThumbailBucketName(thumbnailBucketName); } }while (bucketCreated == false); } else { Console.WriteLine("Required S3 buckets already exist, not creating new ones now"); } }
static UrlType GetUrlType(string url) { UrlType urlType = UrlType.Local; if (AzureHelper.MatchHandler(url)) { urlType = UrlType.Azure; } else if (AzureHelper.MatchFileHandler(url)) { urlType = UrlType.AzureFile; } else if (S3Helper.MatchHandler(url)) { urlType = UrlType.S3; } else if (SkyDriveHelper.MatchHandler(url)) { urlType = UrlType.SkyDrive; } else if (SharepointHelper.MatchHandler(url)) { urlType = UrlType.Sharepoint; } else if (DropboxHelper.MatchHandler(url)) { urlType = UrlType.Dropbox; } else { urlType = UrlType.Local; // local filesystem. } return(urlType); }
public SoftwareDeliveryService() { s3ParamsHelper = new S3Helper( GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color, parameterbucket ); s3CloudFormationHelper = new S3Helper( GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color, cloudFormationBucket ); s3ArtifactHelper = new S3Helper( GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color, GetArtifactS3Bucket() ); lambdaHelper = new LambdaHelper( GlobalVariables.Enviroment, GlobalVariables.Region, GlobalVariables.Color); }
/// <summary> /// Uses machine role permissions. /// </summary> /// <param name="_awsRegion">Aws region.</param> /// <param name="_bucket">Bucket.</param> /// <param name="_roleName">If specified will use the requested role.</param> public S3NoSqlEngine( string _awsRegion, string _dataBucket, string _indexBucket, string _database, string _roleName = null) { m_AwsRegion = _awsRegion; AWSCredentials credentials; if (_roleName == null) { credentials = new InstanceProfileAWSCredentials(); } else { credentials = new InstanceProfileAWSCredentials(_roleName); } m_S3Client = new AmazonS3Client(credentials, S3Helper.GetRegionEndpoint(m_AwsRegion)); DataBucket = _dataBucket; IndexBucket = _indexBucket; Database = _database; }
public void PutFileObjectTest() { // Get the client details from the stored client details (rather than embed secret keys in the test). // Ensure that your AWS/Secret keys have been stored before running. var store = new ClientDetailsStore(); AwsClientDetails clientDetails = store.Load(Container); S3Helper helper = new S3Helper(clientDetails); const string bucketName = "ExampleTestBucket"; const string key = "ExampleObject"; // Put a simple text object into the bucket to delete. helper.CreateBucket(bucketName); try { helper.PutFileObject(bucketName, key, @"C:\Temp\IMD.WebSite.zip"); } catch (Exception ex) { } finally { //helper.DeleteObject(bucketName, key); //helper.DeleteBucket(bucketName); } }
public async Task <IActionResult> EditProfile(EditProfileViewModel model, IFormFile ImageData) { var user = await _userManager.GetUserAsync(User); // Save ImageData Stream fileStream = null; var contentType = ""; if (ImageData != null) // ImageData er: IFormFile ImageData { contentType = ImageData.ContentType; fileStream = ImageData.OpenReadStream(); var ImageExtension = ImageData.FileName.Substring(ImageData.FileName.LastIndexOf(".")).ToLower(); var ImageName = user.Id; // gæti verið t.d. userid? //Save Image to bucket // Add Image to S3 bucket S3Helper s3Helper = new S3Helper(_s3Client); await s3Helper.SaveFile(_imageBucket, "images/" + ImageName + ImageExtension, fileStream, S3CannedACL.PublicRead, contentType); model.Image = ImageName + ImageExtension; } user.FirstName = model.FirstName; user.LastName = model.LastName; user.Address = model.Address; user.FavoriteBook = model.FavoriteBook; user.Image = model.Image; await _userManager.UpdateAsync(user); return(View(model)); }
private void InsertDocument(string _collectionName, BsonDocument doc) { BsonValue id; // if no _id, add one as ObjectId if (!doc.RawValue.TryGetValue("_id", out id)) { doc["_id"] = id = ObjectId.NewObjectId(); } // test if _id is a valid type if (id.IsNull || id.IsMinValue || id.IsMaxValue) { throw S3NoSqlException.InvalidDataType("_id", id); } //m_Log.Write(Logger.COMMAND, "insert document on '{0}' :: _id = {1}", col.CollectionName, id); // serialize object var bytes = BsonSerializer.Serialize(doc); using (MemoryStream stream = new MemoryStream(bytes)) { S3Helper.WriteDocument(m_S3Client, DataBucket, Database, _collectionName, id.AsString, "application/ubjson", stream); } //TODO Queue calculation of the indexes for this document }
public async Task <ServiceResponse <FileUpload> > UploadImageAsync(Stream inputStream, string fileName, long fileLength, string contentType) { var response = new ServiceResponse <FileUpload>(); try { fileName = $"{Guid.NewGuid()}{DateTime.Now.ToString("yyyymmddMMss")}{fileName}"; S3Helper s3 = new S3Helper(_config); string url = await s3.UploadImageAsync(inputStream, fileName, fileLength); var fileUpload = new FileUpload { Name = fileName, ContentType = contentType, Url = url, }; response.Data = fileUpload; return(response); } catch (Exception ex) { _logger.LogError(ex.Message, ex.StackTrace); response.Success = false; response.Message = ex.Message; response.Code = ErrorCode.FILE_UPLOAD_ERROR; return(response); } }
/// <summary> /// Imports cards from S3 jsons /// - Imports for a single page /// </summary> /// <param name="page"></param> /// <param name="pageSize"></param> public void ImportCards(int page, int pageSize) { LambdaLogger.Log($"Entering: ImportCards({page}, {pageSize})"); try { //Retrieving import entries var importCardList = new List <ImportCardDto>(); var ioCards = JsonConvert.DeserializeObject <List <ImportCardDto> >(S3Helper.GetFile(new S3GetFileRequest { FilePath = $"Imports/CardImports/{page}.json" }).Result.FileContent); importCardList.AddRange(ioCards); //Building dao entries var cardList = new List <Card>(); LambdaLogger.Log($"Saving Card List: { JsonConvert.SerializeObject(importCardList) }"); var tcgService = ServiceFactory.GetService <TCGService>(); int i = 0; foreach (var ioCard in importCardList.Where(x => x.Border == null)) { if (ioCard != null) { var newCard = ioCard.Map(ioCard); var floatIdString = $"{page}.{i}"; newCard.FloatId = float.Parse(floatIdString); //tcgService.AddTCGDetails(newCard); newCard.Tags = String.Join(" // ", TranslateCardText(newCard.CardText)); cardList.Add(newCard); } i++; } LambdaLogger.Log($"About to add TCG Price: { JsonConvert.SerializeObject(cardList.Where(x => x.TCGProductId != 0)) }"); //tcgService.AddTCGPrice(cardList); SvcContext.Repository .Cards .Save(cardList); } catch (Exception exp) { LambdaLogger.Log($"Error: { exp }"); throw; } LambdaLogger.Log($"Leaving: ImportCards({page}, {pageSize})"); }
public IntegrationTestContextFixture() { _cloudFormationHelper = new CloudFormationHelper(new AmazonCloudFormationClient(Amazon.RegionEndpoint.USWest2)); _s3Helper = new S3Helper(new AmazonS3Client(Amazon.RegionEndpoint.USWest2)); LambdaHelper = new LambdaHelper(new AmazonLambdaClient(Amazon.RegionEndpoint.USWest2)); CloudWatchHelper = new CloudWatchHelper(new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2)); HttpClient = new HttpClient(); }
public byte[] ReadPage(uint pageID) { Task <Stream> task = S3Helper.ReadPage(m_S3Client, m_BucketName, m_DatabaseName, pageID); Task.WaitAll(task); return(task.Result.ReadToEnd()); }
public async Task <ActionResult> ModifyProfile([Bind(Exclude = "UserPhoto")] ProfileViewModel model) { var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); HttpPostedFileBase file = null; string key = string.Empty; byte[] imageData = null; IdentityResult result = null; if (Request.Files.Count > 0) { file = Request.Files["UserPhoto"]; using (var binary = new BinaryReader(file.InputStream)) { imageData = binary.ReadBytes(file.ContentLength); } } if (user != null && file != null) { if (AppConfig.S3Enable) { // var userId=User.Identity.GetUserId(); var fileName = Guid.NewGuid().ToString(); //key= S3Helper.UploadToS3(file,fileName); S3Helper.sendMyFileToS3(file, fileName); user.ProfileKey = fileName; user.FullName = model.FullName; result = await UserManager.UpdateAsync(user); } else { // To convert the user uploaded Photo as Byte Array before save to DB //Here we pass the byte array to user context to store in db user.UserPhoto = imageData; user.FullName = model.FullName; result = await UserManager.UpdateAsync(user); } // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); if (result.Succeeded) { return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
internal BsonDocument GetById(string _collectionName, string _id) { using (Stream stream = S3Helper.ReadDocument(m_S3Client, DataBucket, Database, _collectionName, _id)) { byte[] data = stream.ReadToEnd(); BsonDocument doc = BsonSerializer.Deserialize(data); return(doc); } }
public void DeleteBucket_Should_Succeed() { // Get the client details from the stored client details (rather than embed secret keys in the test). // Ensure that your AWS/Secret keys have been stored before running. var store = new ClientDetailsStore(); AwsClientDetails clientDetails = store.Load(Container); S3Helper helper = new S3Helper(clientDetails); helper.DeleteBucket("ExampleTestBucket"); }
private static string ModifyUrl(string outputUrl, UrlType outputUrlType) { if (outputUrlType == UrlType.S3) { return(S3Helper.FormatUrl(outputUrl)); } else { return(outputUrl); } }
public byte[] GetThumbnailImage(Guid _fileGuid, string _extension) { string bucketName = Config.GetSettingValue("S3Bucket_Images"); string folderName = Config.GetSettingValue("S3MediaRootFolder"); string filename = ImageProcessingHelper.GetPath(_fileGuid, folderName, _extension, true); var s3 = S3Helper.GetS3(); return(S3Helper.GetFile(s3, bucketName, filename)); }
public void PersistThumbnailImage(Guid _fileGuid, string _extension, string _mimetype, byte[] _data) { string bucketName = Config.GetSettingValue("S3Bucket_Images"); string folderName = Config.GetSettingValue("S3MediaRootFolder"); string filename = ImageProcessingHelper.GetPath(_fileGuid, folderName, _extension, true); var s3 = S3Helper.GetS3(); S3Helper.PersistFile(s3, bucketName, filename, _mimetype, _data); }
public void Put() { var date = new DateTime(2000, 01, 01, 00, 00, 00, DateTimeKind.Utc); string url = S3Helper.GetPresignedUrl( new GetPresignedUrlRequest("PUT", "us-east-1.s3.aws.com", AwsRegion.USEast1, "test", "hi.txt", TimeSpan.FromMinutes(10)), credential, date ); Assert.Equal("https://us-east-1.s3.aws.com/test/hi.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=test%2F20000101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20000101T000000Z&X-Amz-Expires=600&X-Amz-SignedHeaders=host&X-Amz-Signature=e0fa58692735cb96ec84db30112ea47b3f798955bc9803f461698ef670b69c3a", url); }
public void Get() { var date = new DateTime(2000, 01, 01, 00, 00, 00, DateTimeKind.Utc); string url = S3Helper.GetPresignedUrl( new GetPresignedUrlRequest("GET", "us-east-1.s3.aws.com", AwsRegion.USEast1, "test", "hi.txt", TimeSpan.FromMinutes(10)), credential, date ); Assert.Equal("https://us-east-1.s3.aws.com/test/hi.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=test%2F20000101%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20000101T000000Z&X-Amz-Expires=600&X-Amz-SignedHeaders=host&X-Amz-Signature=55cb96b2f5a8c36b168294b3dc1081b939b1674ca89c97deced6e31b93ec5be2", url); }
internal IEnumerable <BsonDocument> GetAll(string _collectionName) { var ids = S3Helper.ListIds(m_S3Client, DataBucket, Database, _collectionName); List <BsonDocument> docs = new List <BsonDocument>(); foreach (var id in ids) { BsonDocument doc = GetById(_collectionName, id); yield return(doc); } }
public ActionResult Delete(int id) { S3Helper s3 = new S3Helper(); var model = _context.Artefacts.FirstOrDefault(x => x.Id == id); var filename = model.Filename; s3.DeleteFile(filename); _context.Artefacts.Remove(_context.Artefacts.FirstOrDefault(x => x.Id == id)); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public ImportService() { Dictionary = new KeywordDictionary(); var s3Response = S3Helper.GetFile(new S3GetFileRequest() { FilePath = MTGServiceConstants.KeywordDictionaryFilepath }).Result; if (s3Response.IsSuccess) { Dictionary = JsonConvert.DeserializeObject <KeywordDictionary>(s3Response.FileContent); } }
/// <summary> /// Imports cards from S3 jsons /// - Imports for a single page /// </summary> /// <param name="page"></param> /// <param name="pageSize"></param> public void ImportCards(int page, int pageSize) { LambdaLogger.Log($"Entering: ImportCards({page}, {pageSize})"); try { //Retrieving import entries var importCardList = new List <ImportCardDto>(); var ioCards = JsonConvert.DeserializeObject <List <ImportCardDto> >(S3Helper.GetFile(new S3GetFileRequest { FilePath = $"Imports/CardImports/{page}.json" }).Result.FileContent); importCardList.AddRange(ioCards); //Building dao entries var cardList = new List <Card>(); LambdaLogger.Log($"Saving Card List: { JsonConvert.SerializeObject(importCardList) }"); int i = 0; foreach (var ioCard in importCardList) { if (ioCard != null) { var newCard = ioCard.Map(ioCard); var floatIdString = $"{page}.{i}"; newCard.FloatId = float.Parse(floatIdString); SvcContext.Repository .Cards .Save(new List <Card>() { newCard }); } i++; } } catch (Exception exp) { LambdaLogger.Log($"Error: { exp }"); throw; } LambdaLogger.Log($"Leaving: ImportCards({page}, {pageSize})"); }
public void CreateDeck(string userId, Deck dto) { string fileContent = JsonConvert.SerializeObject(dto); LambdaLogger.Log($"File Content: { JsonConvert.SerializeObject(dto) }"); var request = new S3CreateFileRequest() { FilePath = string.Format("Users/{0}/Decks/{1}.json", userId, dto.id), Content = fileContent }; var response = S3Helper.CreateFile(request).Result; }