protected void CFProvidersCreateObjectFromFile(string cfcontainername, string cfcreateobjfilepath, string cfcreateobjfilename, int cfcreateobjchunksize, string dcregion, bool dcsnet = true)
        {
            var identity = new RackspaceCloudIdentity() { Username = CFUsernameText.Text, APIKey = CFApiKeyText.Text };

            CloudIdentityProvider identityProvider = new net.openstack.Providers.Rackspace.CloudIdentityProvider(identity);
            CloudFilesProvider CloudFilesProvider = new net.openstack.Providers.Rackspace.CloudFilesProvider(identity);

            CloudFilesProvider.CreateObjectFromFile(cfcontainername, cfcreateobjfilepath, cfcreateobjfilename, cfcreateobjchunksize, null, dcregion, null, dcsnet);
        }
        static void Main(string[] args)
        {
            Console.WriteLine();
            if (ParseArguments(args))
            {
                DirectoryInfo directory = null;

                try
                {
                    directory = new DirectoryInfo(Source);
                    if (!directory.Exists)
                    {
                        throw new Exception(String.Format("Source directory does not exist\n{0}", Source));
                    }
                }
                catch (Exception ex)
                {
                    PrintException(ex);
                }

                Console.WriteLine(String.Format("Source directory: {0}", Source));
                Console.WriteLine(String.Format("Destination container: {0}", Container));
                Console.WriteLine("Logging in...");
                Console.WriteLine();

                if (Login())
                {
                    var cloudFiles = new CloudFilesProvider(auth);

                    try
                    {
                        switch (cloudFiles.CreateContainer(Container))
                        {
                            case ObjectStore.ContainerCreated:
                            case ObjectStore.ContainerExists:
                                foreach (var file in directory.GetFiles())
                                {
                                    Console.WriteLine(String.Format("{0,3:0}% Uploading: {1}", 0, file.Name));
                                    cloudFiles.CreateObjectFromFile(Container, file.FullName, progressUpdated: delegate(long p)
                                    {
                                        Console.SetCursorPosition(0, Console.CursorTop -1);
                                        Console.WriteLine(String.Format("{0,3:0}% Uploading: {1}", ((float)p / (float)file.Length) * 100, file.Name));
                                    });
                                }
                                break;
                            default:
                                throw new Exception(String.Format("Unknown error when creating container {0}", Container));
                        }
                    }
                    catch (Exception ex)
                    {
                        PrintException(ex);
                    }
                }
            }

            Console.WriteLine();
            Console.Write("Press any key to exit");
            Console.ReadKey();
        }
        public void Should_Create_Object_From_File_With_Headers()
        {
            string filePath = Path.Combine(Directory.GetCurrentDirectory(), objectName);
            string fileName = Path.GetFileName(filePath);
            Stream stream = System.IO.File.OpenRead(filePath);
            var etag = GetMD5Hash(filePath);
            stream.Position = 0;
            var headers = new Dictionary<string, string>();
            headers.Add("ETag", etag);
            int cnt = 0;
            var info = new FileInfo(filePath);
            var totalBytest = info.Length;
            var provider = new CloudFilesProvider(_testIdentity);
            provider.CreateObjectFromFile(containerName, filePath, fileName, 65536, headers, null, (bytesWritten) =>
            {
                cnt = cnt + 1;
                if (cnt % 10 != 0)
                    return;

                var x = (float)bytesWritten / (float)totalBytest;
                var percentCompleted = (float)x * 100.00;

                Console.WriteLine(string.Format("{0:0.00} % Completed (Writen: {1} of {2})", percentCompleted, bytesWritten, totalBytest));
            });

            var containerGetObjectsResponse = provider.ListObjects(containerName, identity: _testIdentity);
            Assert.AreEqual(fileName, containerGetObjectsResponse.Where(x => x.Name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().Name);

            var objectHeadersResponse = provider.GetObjectHeaders(containerName, fileName, identity: _testIdentity);

            Assert.IsNotNull(objectHeadersResponse);
            Assert.AreEqual(etag, objectHeadersResponse.Where(x => x.Key.Equals("ETag", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().Value);
        }
        public void Should_Create_Object_From_File_Without_Headers()
        {
            string filePath = Path.Combine(Directory.GetCurrentDirectory(), objectName);
            string fileName = Path.GetFileName(filePath);
            var headers = new Dictionary<string, string>();
            var provider = new CloudFilesProvider(_testIdentity);
            provider.CreateObjectFromFile(containerName, filePath, fileName, 65536, headers, identity: _testIdentity);

            var containerGetObjectsResponse = provider.ListObjects(containerName, identity: _testIdentity);
            Assert.AreEqual(fileName, containerGetObjectsResponse.Where(x => x.Name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().Name);
        }
        public bool UploadToRackSpaceCloudFiles()
        {
            bool syncSucceeded = true;
            try
            {
                var cloudIdentity = new CloudIdentity() { APIKey = this.apiKey, Username = this.username };
                var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);

                List<string> fileList = AmazonS3Helper.ListFiles(localSource);

                foreach (string file in fileList)
                {
                    cloudFilesProvider.CreateObjectFromFile(container, file, Path.GetFileName(file));
                    // assuming this overwrites file if it exists.
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception in uploading to rackspace: "+e);
                syncSucceeded = false;
            }

            return syncSucceeded;
        }
        public void Should_Upload_File_In_Segments()
        {
            var provider = new CloudFilesProvider(_testIdentity);

            string filePath = Path.Combine(Directory.GetCurrentDirectory(), objectName);

            provider.CreateObjectFromFile(containerName2, filePath);

            var objects = provider.ListObjects(containerName2).ToArray();

            Assert.AreEqual(12, objects.Count());
            Assert.IsTrue(objects.Any(o => o.Name.Equals(objectName)));
            for (int i = 0; i < 11; i++)
            {
                Assert.IsTrue(objects.Any(o => o.Name.Equals(string.Format("{0}.seg{1}", objectName, i.ToString("0000")))));
            }
        }
Exemple #7
0
        public static void Main(string[] args)
        {
            Boolean containerExists = false;
            if (args.Length < 4 || args.Length > 5)
            {
                Console.WriteLine("Usage: {0} username api_key target_container path_to_file [region (US|UK)]", Environment.CommandLine);
                Environment.Exit(1);
            }
            RackspaceCloudIdentity auth = new RackspaceCloudIdentity();
            IEnumerable<Container> containerList = null;
            auth.Username = args[0];
            auth.APIKey = args[1];
            targetContainer = args[2];
            filePath = args[3];
            if (args.Length == 5)
            {
                if (args[4] != "UK" && args[4] != "US")
                {
                    Console.WriteLine("region must be either US or UK", Environment.CommandLine);
                    Environment.Exit(1);
                }
                switch (args[4])
                {
                    case "UK": {auth.CloudInstance = CloudInstance.UK;};break;
                    case "US": { auth.CloudInstance = CloudInstance.Default;}; break;
                }
            }

            try
            {
                IIdentityProvider identityProvider = new CloudIdentityProvider();
                var userAccess = identityProvider.Authenticate(auth);
            }
            catch (ResponseException ex2)
            {
                Console.WriteLine("Authentication failed with the following message: {0}",ex2.Message);
                Environment.Exit(1);
            }

            try
            {
                var cloudFilesProvider = new CloudFilesProvider(auth);
                containerList = cloudFilesProvider.ListContainers();

                foreach (Container container in containerList)
                {
                    if (container.Name == targetContainer)
                    {
                        containerExists = true;
                        break;
                    }
                }

                if (!containerExists)
                {
                    Console.WriteLine("Container \"{0}\" does not exist on the provided CloudFiles account.", targetContainer);
                    Environment.Exit(1);
                }
                if (!File.Exists(filePath))
                {
                    Console.WriteLine("The file specified ({0}) does not exist", filePath);
                    Environment.Exit(1);
                }
                cloudFilesProvider.CreateObjectFromFile(targetContainer, @filePath, Path.GetFileName(filePath));
            }
            catch (Exception ex2)
            {
                Console.WriteLine(ex2.Message);
                Environment.Exit(1);
            }
            Console.WriteLine("*SUCCESS* File: \"{0}\" uploaded to \"{1}\"", filePath, targetContainer);
        }
 private void UploadObject(CloudIdentity cloudIdentity, string container, string objectname, string filename) {
     var provider = new CloudFilesProvider(cloudIdentity);
     provider.CreateObjectFromFile(container: container, filePath: filename, objectName: objectname, progressUpdated: ProgressCallback);
 }
        public void TestCreateLargeObject()
        {
            IObjectStorageProvider provider =
                new CloudFilesProvider(Bootstrapper.Settings.TestIdentity)
                {
                    LargeFileBatchThreshold = 81920
                };

            string containerName = TestContainerPrefix + Path.GetRandomFileName();
            string sourceFileName = "DarkKnightRises.jpg";
            byte[] content = File.ReadAllBytes("DarkKnightRises.jpg");

            ObjectStore containerResult = provider.CreateContainer(containerName);
            Assert.AreEqual(ObjectStore.ContainerCreated, containerResult);

            ProgressMonitor progressMonitor = new ProgressMonitor(content.Length);
            provider.CreateObjectFromFile(containerName, sourceFileName, progressUpdated: progressMonitor.Updated);
            Assert.IsTrue(progressMonitor.IsComplete, "Failed to notify progress monitor callback of status update.");

            using (MemoryStream downloadStream = new MemoryStream())
            {
                provider.GetObject(containerName, sourceFileName, downloadStream);
                Assert.AreEqual(content.Length, GetContainerObjectSize(provider, containerName, sourceFileName));

                downloadStream.Position = 0;
                byte[] actualData = new byte[downloadStream.Length];
                downloadStream.Read(actualData, 0, actualData.Length);
                Assert.AreEqual(content.Length, actualData.Length);
                using (MD5 md5 = MD5.Create())
                {
                    byte[] contentMd5 = md5.ComputeHash(content);
                    byte[] actualMd5 = md5.ComputeHash(actualData);
                    Assert.AreEqual(BitConverter.ToString(contentMd5), BitConverter.ToString(actualMd5));
                }
            }

            /* Cleanup
             */
            provider.DeleteContainer(containerName, deleteObjects: true);
        }
        public void TestCreateObjectFromFile_UseCustomObjectName()
        {
            IObjectStorageProvider provider = new CloudFilesProvider(Bootstrapper.Settings.TestIdentity);
            string containerName = TestContainerPrefix + Path.GetRandomFileName();
            string objectName = Path.GetRandomFileName();
            // another random name counts as random content
            string fileData = Path.GetRandomFileName();
            string tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            ObjectStore containerResult = provider.CreateContainer(containerName);
            Assert.AreEqual(ObjectStore.ContainerCreated, containerResult);

            try
            {
                File.WriteAllText(tempFilePath, fileData, Encoding.UTF8);
                provider.CreateObjectFromFile(containerName, tempFilePath, objectName);

                // it's ok to create the same file twice
                ProgressMonitor progressMonitor = new ProgressMonitor(new FileInfo(tempFilePath).Length);
                provider.CreateObjectFromFile(containerName, tempFilePath, objectName, progressUpdated: progressMonitor.Updated);
                Assert.IsTrue(progressMonitor.IsComplete, "Failed to notify progress monitor callback of status update.");
            }
            finally
            {
                File.Delete(tempFilePath);
            }

            string actualData = ReadAllObjectText(provider, containerName, objectName, Encoding.UTF8);
            Assert.AreEqual(fileData, actualData);

            /* Cleanup
             */
            provider.DeleteContainer(containerName, deleteObjects: true);
        }
        public void Should_Create_Object_From_File_With_Headers()
        {
            string filePath = Path.Combine(Directory.GetCurrentDirectory(), objectName);
            var etag = GetMD5Hash(filePath);
            var headers = new Dictionary<string, string> {{"ETag", etag}};
            var provider = new CloudFilesProvider(_testIdentity);
            provider.CreateObjectFromFile(containerName, filePath, objectName, headers: headers);

            var containerGetObjectsResponse = provider.ListObjects(containerName, identity: _testIdentity);
            Assert.AreEqual(objectName, containerGetObjectsResponse.Where(x => x.Name.Equals(objectName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().Name);

            var objectHeadersResponse = provider.GetObjectHeaders(containerName, objectName, identity: _testIdentity);

            Assert.IsNotNull(objectHeadersResponse);
            Assert.AreEqual(etag, objectHeadersResponse.Where(x => x.Key.Equals("ETag", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().Value);
        }