Esempio n. 1
0
      public void Add(Stream data, string key, string contentType)
      {
         //using (ObjectAddRequest objectAddRequest = new ObjectAddRequest(S3Storage.BucketName, key))
         //{
         //   byte[] buffer = new byte[data.Length];

         //   data.Read(buffer, 0, (int)data.Length);

         //   objectAddRequest.LoadStreamWithBytes(buffer, contentType);

         //   using (ObjectAddResponse objectAddResponse = _service.ObjectAdd(objectAddRequest))
         //   { }
         //}

         // The first thing we need to do is check for the presence of a Temporary Redirect.  These occur for a few
         // minutes after an EU bucket is created, while S3 creates the DNS entries.  If we get one, we need to upload
         // the file to the redirect URL

         try
         {
            String redirectUrl = null;
            using (BucketListRequest testRequest = new BucketListRequest(BucketName))
            {
               testRequest.Method = "HEAD";
               using (BucketListResponse testResponse = _service.BucketList(testRequest))
               {
                  if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                  {
                     redirectUrl = testResponse.Headers["Location"].ToString() + key;
                  }
               }
            }

            using (ObjectAddRequest request = new ObjectAddRequest(BucketName, key))
            {
               //request.Headers.Add("x-amz-acl", "public-read-write");
               //request.ContentType = ThreeSharpUtils.ConvertExtensionToMimeType(Path.GetExtension(strKey));
               //request.DataStream = oStream;
               //request.BytesTotal = (request.DataStream).Length;
               //request.Timeout = iTimeout;

               byte[] buffer = new byte[data.Length];
               data.Read(buffer, 0, (int)data.Length);
               request.LoadStreamWithBytes(buffer, contentType);

               if (redirectUrl != null)
               {
                  request.RedirectUrl = redirectUrl;
               }

               using (ObjectAddResponse response = _service.ObjectAdd(request))
               { }
            }
         }
         catch (ThreeSharpException exception)
         {
            throw new BinaryRepositoryException("Upload Failed", exception);
         }
      }
Esempio n. 2
0
 /// <summary>
 /// Returns a string of XML, describing the contents of a bucket
 /// </summary>
 public String ListBucket(String bucketName)
 {
     using (BucketListRequest bucketListRequest = new BucketListRequest(bucketName))
     using (BucketListResponse bucketListResponse = this.service.BucketList(bucketListRequest))
     {
         return bucketListResponse.StreamResponseToString();
     }
 }
Esempio n. 3
0
        private void DownloadFile(String bucketName, String key)
        {
            // The first thing we need to do is check for the presence of a Temporary Redirect.  These occur for a few
            // minutes after an EU bucket is created, while S3 creates the DNS entries.  If we get one, we need to download
            // the file from the redirect URL

            String redirectUrl = null;
            using (BucketListRequest testRequest = new BucketListRequest(bucketName))
            {
                testRequest.Method = "HEAD";
                using (BucketListResponse testResponse = service.BucketList(testRequest))
                {
                    if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                    {
                        redirectUrl = testResponse.Headers["Location"].ToString() + key;
                    }
                }
            }

            using (ObjectGetRequest request = new ObjectGetRequest(bucketName, key))
            {
                request.RedirectUrl = redirectUrl;
                using (ObjectGetResponse response = service.ObjectGet(request))
                {
                    response.StreamResponseToFile(this.basePath + key);
                }
            }

            this.listBoxFolder.Invoke(listFolderDelegate, new object[] { this.basePath });
        }
Esempio n. 4
0
        private void UploadFile(String bucketName, String filename)
        {
            // The first thing we need to do is check for the presence of a Temporary Redirect.  These occur for a few
            // minutes after an EU bucket is created, while S3 creates the DNS entries.  If we get one, we need to upload
            // the file to the redirect URL

            String redirectUrl = null;
            using (BucketListRequest testRequest = new BucketListRequest(bucketName))
            {
                testRequest.Method = "HEAD";
                using (BucketListResponse testResponse = service.BucketList(testRequest))
                {
                    if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                    {
                        redirectUrl = testResponse.Headers["Location"].ToString() + filename.Replace(' ', '_');
                    }
                }
            }

            using (ObjectAddRequest request = new ObjectAddRequest(bucketName, filename.Replace(' ', '_')))
            {
                request.Headers.Add("x-amz-acl", Properties.Settings.Default.AclType);
                request.LoadStreamWithFile(this.basePath + filename);

                if (redirectUrl != null)
                {
                    request.RedirectUrl = redirectUrl;
                }

                using (ObjectAddResponse response = service.ObjectAdd(request))
                { }
            }

            this.listBoxObjects.Invoke(listBucketDelegate, new object[] { bucketName });
        }
Esempio n. 5
0
        private void ListBucket(String bucketName)
        {
            try
            {
                this.listBoxObjects.Items.Clear();

                // The first thing we need to do is check for the presence of a Temporary Redirect.  These occur for a few
                // minutes after an EU bucket is created, while S3 creates the DNS entries.  If we get one, we need to pull
                // the bucket listing from the redirect URL

                String redirectUrl = null;
                using (BucketListRequest testRequest = new BucketListRequest(bucketName))
                {
                    testRequest.Method = "HEAD";
                    using (BucketListResponse testResponse = service.BucketList(testRequest))
                    {
                        if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                        {
                            redirectUrl = testResponse.Headers["Location"].ToString();
                        }
                    }
                }

                bool isTruncated = true;
                string marker = string.Empty;

                // The while-loop is here because S3 will only return a maximum of 1000 items at a time, so if the list
                // was truncated, we need to make another call and get the rest
                while (isTruncated)
                {
                    using (BucketListRequest request = new BucketListRequest(bucketName))
                    {
                        request.RedirectUrl = redirectUrl;
                        if (!string.IsNullOrEmpty(marker))
                        {
                            request.QueryList.Add("marker", marker);
                        }
                        using (BucketListResponse response = service.BucketList(request))
                        {
                            XmlDocument bucketXml = response.StreamResponseToXmlDocument();

                            XmlNodeList objects = bucketXml.SelectNodes("//*[local-name()='Key']");
                            foreach (XmlNode obj in objects)
                            {
                                marker = obj.InnerText;
                                this.listBoxObjects.Items.Add(marker);
                            }

                            isTruncated = bool.Parse(bucketXml.SelectSingleNode("//*[local-name()='IsTruncated']").InnerXml);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 6
0
        private void BindBucketNames()
        {
            this.comboBoxBucketNames.Items.Clear();

            using (BucketListRequest request = new BucketListRequest(null))
            using (BucketListResponse response = service.BucketList(request))
            {
                XmlDocument bucketXml = response.StreamResponseToXmlDocument();

                XmlNodeList buckets = bucketXml.SelectNodes("//*[local-name()='Name']");
                foreach (XmlNode bucket in buckets)
                {
                    this.comboBoxBucketNames.Items.Add(bucket.InnerXml);
                }
            }
        }
Esempio n. 7
0
        private void buttonDeleteObject_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listBoxObjects.SelectedItem == null)
                    return;

                ConfirmDeleteForm confirmDeleteForm = new ConfirmDeleteForm(this.listBoxObjects.SelectedItems.Count.ToString());
                if (confirmDeleteForm.ShowDialog() != DialogResult.OK)
                    return;

                foreach (object item in listBoxObjects.SelectedItems)
                {
                    // The first thing we need to do is check for the presence of a Temporary Redirect.  These occur for a few
                    // minutes after an EU bucket is created, while S3 creates the DNS entries.  If we get one, we need to delete
                    // the file from the redirect URL

                    String redirectUrl = null;
                    using (BucketListRequest testRequest = new BucketListRequest(this.comboBoxBucketNames.SelectedItem.ToString()))
                    {
                        testRequest.Method = "HEAD";
                        using (BucketListResponse testResponse = service.BucketList(testRequest))
                        {
                            if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                            {
                                redirectUrl = testResponse.Headers["Location"].ToString() + item.ToString();
                            }
                        }
                    }

                    using (ObjectDeleteRequest request = new ObjectDeleteRequest(this.comboBoxBucketNames.SelectedItem.ToString(), item.ToString()))
                    {
                        request.RedirectUrl = redirectUrl;
                        using (ObjectDeleteResponse response = service.ObjectDelete(request))
                        { }
                    }
                }
                this.ListBucket(this.comboBoxBucketNames.SelectedItem.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        
        }
        static void DemonstrateThreeSharp()
        {
            Console.WriteLine("===========================================");
            Console.WriteLine("Demonstrating the ThreeSharp Library");
            Console.WriteLine("===========================================");

            ThreeSharpConfig config = new ThreeSharpConfig();
            config.AwsAccessKeyID = awsAccessKeyId;
            config.AwsSecretAccessKey = awsSecretAccessKey;

            IThreeSharp service = new ThreeSharpQuery(config);

            // Convert the bucket name to lowercase for vanity domains.
            // the bucket must be lower case since DNS is case-insensitive.
            Random r = new Random();
            string testBucketName = awsAccessKeyId.ToLower() + "-test-bucket" + r.Next(50000).ToString();;
            string testBucketName2 = awsAccessKeyId.ToLower() + "-test-bucket" + r.Next(50000).ToString(); ;
            string stringKeyName = "StringObject";
            string encryptedFileKeyName = "EncryptedFileObject";


            Console.WriteLine("\n----- Creating Bucket " + testBucketName + " -----");
            using (BucketAddRequest request = new BucketAddRequest(testBucketName))
            using (BucketAddResponse response = service.BucketAdd(request))
            { }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Creating Bucket " + testBucketName2 + " -----");
            using (BucketAddRequest request = new BucketAddRequest(testBucketName2))
            using (BucketAddResponse response = service.BucketAdd(request))
            { }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName2 + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName2))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Adding String Object to " + testBucketName + " -----");
            using (ObjectAddRequest request = new ObjectAddRequest(testBucketName, stringKeyName))
            {
                request.LoadStreamWithString("This is a string object.");
                using (ObjectAddResponse response = service.ObjectAdd(request))
                { }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Getting String Object -----");
            using (ObjectGetRequest request = new ObjectGetRequest(testBucketName, stringKeyName))
            {
                using (ObjectGetResponse response = service.ObjectGet(request))
                {
                    Console.WriteLine(response.StreamResponseToString());
                }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Copying String Object to " + testBucketName2 + " -----");
            using (ObjectCopyRequest request = new ObjectCopyRequest(testBucketName, stringKeyName, testBucketName2, stringKeyName + "Copy"))
            using (ObjectCopyResponse response = service.ObjectCopy(request))
            {
                int i = 0;
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName2 + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName2))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Getting Copied String Object -----");
            using (ObjectGetRequest request = new ObjectGetRequest(testBucketName2, stringKeyName + "Copy"))
            {
                using (ObjectGetResponse response = service.ObjectGet(request))
                {
                    Console.WriteLine(response.StreamResponseToString());
                }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();


            Console.WriteLine("\n----- Streaming File Object -----");
            using (ObjectAddRequest request = new ObjectAddRequest(testBucketName, uploadFile))
            {
                request.LoadStreamWithFile(uploadPath + uploadFile);
                using (ObjectAddResponse response = service.ObjectAdd(request))
                { }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Streaming/Encrypting File Object -----");
            using (ObjectAddRequest request = new ObjectAddRequest(testBucketName, uploadFile + "Encrypted"))
            {
                request.LoadStreamWithFile(uploadPath + uploadFile);
                request.EncryptStream(encryptionKey, encryptionIV);
                using (ObjectAddResponse response = service.ObjectAdd(request))
                { }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Streaming File to Disk -----");
            using (ObjectGetRequest request = new ObjectGetRequest(testBucketName, uploadFile))
            using (ObjectGetResponse response = service.ObjectGet(request))
            {
                response.StreamResponseToFile(downloadPath + uploadFile);
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Streaming/Decrypting File to Disk -----");
            using (ObjectGetRequest request = new ObjectGetRequest(testBucketName, uploadFile + "Encrypted"))
            using (ObjectGetResponse response = service.ObjectGet(request))
            {
                response.DecryptStream(encryptionKey, encryptionIV);
                response.StreamResponseToFile(downloadPath + "decrypted-" + uploadFile);
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Query String Authorization Example -----");
            using (UrlGetRequest request = new UrlGetRequest(testBucketName, uploadFile))
            {
                request.ExpiresIn = 60 * 1000;
                using (UrlGetResponse response = service.UrlGet(request))
                {
                    Console.WriteLine("Try this url in your web browser (it will only work for 60 seconds)\n");
                    Console.WriteLine(response.StreamResponseToString());
                }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Adding String Object with Metadata and Public Read ACL -----");
            using (ObjectAddRequest request = new ObjectAddRequest(testBucketName, stringKeyName + "Public"))
            {
                request.LoadStreamWithString("This is a publicly readable test.");
                request.MetaData.Add("blah", "foo");
                //request.Headers.Add("x-amz-acl", "private");
                request.Headers.Add("x-amz-acl", "public-read");
                //request.Headers.Add("x-amz-acl", "public-read-write");
                //request.Headers.Add("x-amz-acl", "authenticated-read");
                using (ObjectAddResponse response = service.ObjectAdd(request))
                { }
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Getting Object's ACL -----");
            using (ACLGetRequest request = new ACLGetRequest(testBucketName, stringKeyName + "Public"))
            using (ACLGetResponse response = service.ACLGet(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Deleting Objects -----");
            using (ObjectDeleteRequest request = new ObjectDeleteRequest(testBucketName, stringKeyName))
            using (ObjectDeleteResponse response = service.ObjectDelete(request))
            { }
            using (ObjectDeleteRequest request = new ObjectDeleteRequest(testBucketName2, stringKeyName + "Copy"))
            using (ObjectDeleteResponse response = service.ObjectDelete(request))
            { }
            using (ObjectDeleteRequest request = new ObjectDeleteRequest(testBucketName, uploadFile))
            using (ObjectDeleteResponse response = service.ObjectDelete(request))
            { }
            using (ObjectDeleteRequest request = new ObjectDeleteRequest(testBucketName, uploadFile + "Encrypted"))
            using (ObjectDeleteResponse response = service.ObjectDelete(request))
            { }
            using (ObjectDeleteRequest request = new ObjectDeleteRequest(testBucketName, stringKeyName + "Public"))
            using (ObjectDeleteResponse response = service.ObjectDelete(request))
            { }

            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing Bucket " + testBucketName2 + " -----");
            using (BucketListRequest request = new BucketListRequest(testBucketName2))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing All My Buckets -----");
            using (BucketListRequest request = new BucketListRequest(null))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Deleting Bucket " + testBucketName + " -----");
            using (BucketDeleteRequest request = new BucketDeleteRequest(testBucketName))
            using (BucketDeleteResponse response = service.BucketDelete(request))
            { }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Deleting Bucket " + testBucketName2 + " -----");
            using (BucketDeleteRequest request = new BucketDeleteRequest(testBucketName2))
            using (BucketDeleteResponse response = service.BucketDelete(request))
            { }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\n----- Listing All My Buckets -----");
            using (BucketListRequest request = new BucketListRequest(null))
            using (BucketListResponse response = service.BucketList(request))
            {
                Console.WriteLine(response.StreamResponseToString());
            }
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

            Console.WriteLine("\nTotal data transfers: " + service.GetTransfers().Length.ToString());
            Console.WriteLine("Total bytes uploaded: " + service.GetTotalBytesUploaded().ToString());
            Console.WriteLine("Total bytes downloaded: " + service.GetTotalBytesDownloaded().ToString());
            Console.WriteLine("\npress enter >");
            Console.ReadLine();

        }