Exemple #1
0
        public static void PutPhoto(string domainName, string itemName, string bucketName, string fileName, Stream fileContent, bool isPublic, AmazonSimpleDBClient sdbClient, AmazonS3Client s3Client)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return;
            }

            BucketHelper.CheckForBucket(itemName, s3Client);

            PutObjectRequest putObjectRequest = new PutObjectRequest();

            putObjectRequest.BucketName  = bucketName;
            putObjectRequest.CannedACL   = S3CannedACL.PublicRead;
            putObjectRequest.Key         = fileName;
            putObjectRequest.InputStream = fileContent;
            s3Client.PutObject(putObjectRequest);

            DomainHelper.CheckForDomain(domainName, sdbClient);
            PutAttributesRequest putAttrRequest = new PutAttributesRequest()
            {
                DomainName = domainName,
                ItemName   = itemName
            };

            putAttrRequest.Attributes.Add(new ReplaceableAttribute
            {
                Name    = "PhotoThumbUrl",
                Value   = String.Format(Settings.Default.S3BucketUrlFormat, String.Format(Settings.Default.BucketNameFormat, HttpContext.Current.User.Identity.Name, itemName), fileName),
                Replace = true
            });
            sdbClient.PutAttributes(putAttrRequest);

            if (isPublic)
            {
                DomainHelper.CheckForDomain(Settings.Default.PetBoardPublicDomainName, sdbClient);
                putAttrRequest.DomainName = Settings.Default.PetBoardPublicDomainName;
                sdbClient.PutAttributes(putAttrRequest);
            }
        }
Exemple #2
0
        // Folders can be objects of zero length of content type
        // application/x-www-form-urlencoded;charset=UTF-8 as used by the Storage Browser:
        // https://console.cloud.google.com/storage/browser?project=YOUR-PROJECT-ID
        // Folders can also be implicit in file names with prefixes containing the delimiter "/",
        // also used by the Storage Browser. The extension methods provided in BrowserHelper know
        // how to deal with both.
        static void ListFilesAndFolders(StorageClient client, string bucket, string parentFolder = "", string indent = "  ")
        {
            if (parentFolder == "")
            {
                Console.WriteLine($"Files and folders in bucket {bucket}:");
            }

            string shortName = parentFolder == "" ? bucket : BucketHelper.ShortName(parentFolder);

            Console.WriteLine($"{indent}{shortName}/");
            indent += "  ";

            foreach (var file in client.ListFiles(bucket, parentFolder))
            {
                Console.WriteLine($"{indent}{file.ShortName()} [{file.ContentType}]");
            }

            foreach (var folder in client.ListFolders(bucket, parentFolder))
            {
                ListFilesAndFolders(client, bucket, folder, indent);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session[Settings.Default.FlashSessionKey] != null)
            {
                this.FlashLiteralWrapper.Visible = true;
                this.FlashLiteral.Text           = this.Session[Settings.Default.FlashSessionKey].ToString();
                this.Session[Settings.Default.FlashSessionKey] = null;
            }
            else
            {
                this.FlashLiteralWrapper.Visible = false;
            }

            this._petIdString = this.Request.QueryString["petid"];

            if (String.IsNullOrEmpty(this._petIdString))
            {
                this.StatsLiteral.Text    = "Add a New Pet";
                this.SaveStatsButton.Text = "Save New Pet";
            }
            else
            {
                this.PhotoPanel.Visible = true;
            }

            this._userBucketName = String.Format(Settings.Default.BucketNameFormat, this.Context.User.Identity.Name, this._petIdString);
            this._itemName       = this._petIdString ?? Guid.NewGuid().ToString();
            this._domainName     = String.Format(Settings.Default.SimpleDbDomainNameFormat, this.Context.User.Identity.Name);

            if (!this.Page.IsPostBack)
            {
                List <int> years = new List <int>(100);
                for (int i = 0; i < 100; i++)
                {
                    years.Add(DateTime.Now.AddYears(i * -1).Year);
                }
                this.YearDropDownList.DataSource = years.OrderByDescending(y => y);
                this.YearDropDownList.DataBind();
                this.SelectMonth();
                this.SelectDay();

                Pet           pet   = default(Pet);
                List <string> files = new List <string>();
                if (!String.IsNullOrEmpty(this._petIdString))
                {
                    //
                    // Try to get the requested pet from the user's private domain
                    //
                    DomainHelper.CheckForDomain(this._domainName, _simpleDBClient);

                    GetAttributesRequest getAttributeRequest = new GetAttributesRequest()
                    {
                        DomainName = this._domainName,
                        ItemName   = this._itemName
                    };

                    GetAttributesResponse getAttributeResponse = _simpleDBClient.GetAttributes(getAttributeRequest);
                    List <Attribute>      attrs = null;
                    bool showPublic             = false;

                    attrs = getAttributeResponse.Attributes;

                    showPublic = false;

                    //
                    // If we can't find it try the public domain
                    //
                    if (attrs.Count == 0)
                    {
                        showPublic = true;
                    }

                    if (showPublic)
                    {
                        Response.Redirect(String.Concat("PetProfile.aspx?petid", _petIdString));
                        return;
                    }

                    pet = new Pet
                    {
                        Name      = attrs.First(a => a.Name == "Name").Value,
                        Birthdate = attrs.First(a => a.Name == "Birthdate").Value,
                        Sex       = attrs.First(a => a.Name == "Sex").Value,
                        Type      = attrs.First(a => a.Name == "Type").Value,
                        Breed     = attrs.First(a => a.Name == "Breed").Value,
                        Likes     = attrs.First(a => a.Name == "Likes").Value,
                        Dislikes  = attrs.First(a => a.Name == "Dislikes").Value
                    };

                    this.Public.Checked = bool.Parse(attrs.First(a => a.Name == "Public").Value);

                    using (AmazonS3Client s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))
                    {
                        BucketHelper.CheckForBucket(this._petIdString, s3Client);
                        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                        {
                            BucketName = this._userBucketName
                        };

                        ListObjectsResponse listObjectsResponse = s3Client.ListObjects(listObjectsRequest);
                        {
                            files = listObjectsResponse.S3Objects.Select(o => String.Format(Settings.Default.S3BucketUrlFormat, this._userBucketName, o.Key)).ToList();
                            string firstPhoto = files.FirstOrDefault();
                            this.PhotoThumbUrl.Value = firstPhoto ?? String.Empty;
                        }
                    }
                }

                if (pet != default(Pet))
                {
                    this.PetNameHeader.Text = pet.Name;
                    this.NameTextBox.Text   = pet.Name;
                    this.AnimalDropDownList.SelectedValue = pet.Type;
                    this.BreedTextBox.Text             = pet.Breed;
                    this.SexDropDownList.SelectedValue = pet.Sex;
                    if (pet.Birthdate != null)
                    {
                        DateTime birthdate = DateTime.Parse(pet.Birthdate);
                        this.YearDropDownList.SelectedValue  = birthdate.Year.ToString();
                        this.MonthDropDownList.SelectedValue = birthdate.Month.ToString();
                        this.DayDropDownList.SelectedValue   = birthdate.Day.ToString();
                    }
                    this.LikesTextBox.Text        = pet.Likes;
                    this.DislikesTextBox.Text     = pet.Dislikes;
                    this.PhotoRepeater.DataSource = files;
                    this.PhotoRepeater.DataBind();
                }
            }
        }
 private void SetSecurityInfo()
 {
     this.StoreService.BucketId = BucketHelper.GetBucketId(this.Request);
     this.StoreService.UserId   = GetUserID();
 }
        public void bucketName_should_be_bucket1_when_object_id_starts_with_bucket1_and_slash()
        {
            var obj = BucketHelper.ExtractObjectInfo("bucket1/object-name");

            obj.bucketName.Should().Be("bucket1");
        }
        public void bucketName_should_be_null_when_does_not_contain_bucket_folder_at_start()
        {
            var obj = BucketHelper.ExtractObjectInfo("object-name");

            obj.bucketName.Should().Be(null);
        }
        public void Object_Id_Should_Throw_ObjectIdIsNotValidException_when_containing_not_allowed_characters()
        {
            Action act = () => BucketHelper.ExtractObjectInfo("object-image-%@.jpg");

            act.Should().Throw <ObjectIdIsNotValidException>();
        }