예제 #1
0
 protected void deleteRecursively( ObjectPath path ) {
     if ( path.IsDirectory() ) {
         ListOptions options = new ListOptions();
         do {
             foreach ( DirectoryEntry entry in this.esu.ListDirectory( path, options ) ) {
                 deleteRecursively( entry.Path );
             }
         } while ( options.Token != null );
     }
     this.esu.DeleteObject( path );
 }
예제 #2
0
        public void testWriteAccessToken()
        {
            string dir = rand8char();
            string file = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);
            esu.CreateObjectOnPath(new ObjectPath("/" + dir + "/"), null, null, null, null);

            DateTime expiration = DateTime.UtcNow;
            expiration += TimeSpan.FromMinutes(5);

            SourceType source = new SourceType();
            source.Allow = new string[] { "10.0.0.0/8", "128.0.0.0/8" };
            source.Disallow = new string[] { "1.1.1.1" };

            ContentLengthRangeType range = new ContentLengthRangeType();
            range.From = 0;
            range.To = 1024; // 1KB

            FormFieldType formField1 = new FormFieldType();
            formField1.Name = "x-emc-meta";
            formField1.Optional = true;
            FormFieldType formField2 = new FormFieldType();
            formField2.Name = "x-emc-listable-meta";
            formField2.Optional = true;

            PolicyType policy = new PolicyType();
            policy.Expiration = expiration;
            policy.Source = source;
            policy.MaxDownloads = 2;
            policy.MaxUploads = 1;
            policy.ContentLengthRange = range;
            policy.FormField = new FormFieldType[] { formField1, formField2 };

            Uri tokenUri = esu.CreateAccessToken(op, policy, null);

            // create upload form manually (easiest since we're using HttpWebRequest)
            string content = "Form Upload Test";
            string boundary = "BOUNDARY_1234567890";
            string EOL = "\r\n";
            string payload = "--" + boundary + EOL;
            payload += "Content-Type: text/plain" + EOL;
            payload += "Content-Disposition: form-data; name=\"x-emc-meta\"" + EOL + EOL;
            payload += "color=gray,size=3,foo=bar";
            payload += EOL + "--" + boundary + EOL;
            payload += "Content-Type: text/plain" + EOL;
            payload += "Content-Disposition: form-data; name=\"x-emc-listable-meta\"" + EOL + EOL;
            payload += "listable=";
            payload += EOL + "--" + boundary + EOL;
            payload += "Content-Type: text/plain" + EOL;
            payload += "Content-Disposition: form-data; name=\"data\"; filename=\"foo.txt\"" + EOL + EOL;
            payload += content;
            payload += EOL + "--" + boundary + "--" + EOL;

            WebRequest request = WebRequest.Create(tokenUri);
            request.Method = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            new MemoryStream(Encoding.UTF8.GetBytes(payload)).CopyTo(request.GetRequestStream());
            WebResponse response = request.GetResponse();
            Assert.AreEqual(201, (int) (response as HttpWebResponse).StatusCode, "Wrong status code");
            string path = response.Headers["Location"];
            ObjectId oid = new ObjectId(path.Split('/').Last());
            cleanup.Add(oid);
            response.Close();

            // read back object via token
            response = WebRequest.Create(tokenUri).GetResponse();
            Assert.AreEqual(content, new StreamReader(response.GetResponseStream()).ReadToEnd(), "content from token not equal");
            response.Close();

            // read back object via namespace
            Assert.AreEqual(content, Encoding.UTF8.GetString(esu.ReadObject(op, null, null)), "content from namespace not equal");

            esu.DeleteAccessToken(tokenUri);
        }
예제 #3
0
        public void testReadAccessToken()
        {
            string dir = rand8char();
            string file = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);
            string data = "hello";
            ObjectId id = esu.CreateObjectOnPath(op, null, null, Encoding.UTF8.GetBytes(data), "text/plain");

            DateTime expiration = DateTime.UtcNow;
            expiration += TimeSpan.FromMinutes(5);

            SourceType source = new SourceType();
            source.Allow = new string[] {"10.0.0.0/8", "128.0.0.0/8"};
            source.Disallow = new string[] {"1.1.1.1"};

            ContentLengthRangeType range = new ContentLengthRangeType();
            range.From = 0;
            range.To = 1024; // 1KB

            FormFieldType formField1 = new FormFieldType();
            formField1.Name = "x-emc-meta";
            formField1.Optional = true;
            FormFieldType formField2 = new FormFieldType();
            formField2.Name = "x-emc-listable-meta";
            formField2.Optional = true;

            PolicyType policy = new PolicyType();
            policy.Expiration = expiration;
            policy.Source = source;
            policy.MaxDownloads = 2;
            policy.MaxUploads = 0;
            policy.ContentLengthRange = range;
            policy.FormField = new FormFieldType[] {formField1, formField2};

            Uri tokenUri = esu.CreateAccessToken(id, policy, null);

            WebResponse response = WebRequest.Create(tokenUri).GetResponse();
            string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            response.Close();
            Assert.AreEqual(data, content, "Token URL does not contain proper content");

            esu.DeleteAccessToken(tokenUri);

            tokenUri = esu.CreateAccessToken(op, policy, null);

            response = WebRequest.Create(tokenUri).GetResponse();
            content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            response.Close();
            Assert.AreEqual(data, content, "Token URL does not contain proper content");

            policy.MaxDownloads = policy.MaxDownloads - 1; // we already used one

            ListOptions options = new ListOptions();
            List<AccessTokenType> tokens = esu.ListAccessTokens(options);
            Assert.AreEqual(1, tokens.Count, "ListTokens returns wrong count");
            AssertTokenPolicy(tokens[0], policy);

            AccessTokenType token = esu.GetAccessToken(tokenUri);

            esu.DeleteAccessToken(token.AccessTokenId);

            string path = tokenUri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
            Assert.AreEqual(path.Split('/').Last(), token.AccessTokenId, "token ID doesn't match");
            AssertTokenPolicy(token, policy);
        }
예제 #4
0
        public void testCreateObjectUnicodePath()
        {
            string dir = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/спасибо.txt");
            MemoryStream ms = new MemoryStream();

            ObjectId id = this.esu.CreateObjectOnPath(op, null, null, Encoding.UTF8.GetBytes("спасибо"), "text/plain", null);
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);

            // Read back the content
            string content = Encoding.UTF8.GetString(this.esu.ReadObject(id, null, null));
            Assert.AreEqual("спасибо", content, "object content wrong");
        }
예제 #5
0
        public void testRename()
        {
            ObjectPath op1 = new ObjectPath("/" + rand8char() + ".tmp");
            ObjectPath op2 = new ObjectPath("/" + rand8char() + ".tmp");
            ObjectPath op3 = new ObjectPath("/" + rand8char() + ".tmp");
            ObjectPath op4 = new ObjectPath("/" + rand8char() + ".tmp");
            ObjectId id = this.esu.CreateObjectOnPath(op1, null, null, 
                Encoding.UTF8.GetBytes("Four score and seven years ago"), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);

            // Rename the object
            this.esu.Rename(op1, op2, false);

            // Read back the content
            string content = Encoding.UTF8.GetString(this.esu.ReadObject(op2, null, null));
            Assert.AreEqual("Four score and seven years ago", content, "object content wrong");

            // Attempt overwrite
            id = this.esu.CreateObjectOnPath(op3, null, null,
                Encoding.UTF8.GetBytes("Four score and seven years ago"), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);
            id = this.esu.CreateObjectOnPath(op4, null, null,
                Encoding.UTF8.GetBytes("You shouldn't see me"), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);
            this.esu.Rename(op3, op4, true);

            // Wait for rename to complete
            System.Threading.Thread.Sleep(5000);

            // Read back the content
            content = Encoding.UTF8.GetString(this.esu.ReadObject(op4, null, null));
            Assert.AreEqual("Four score and seven years ago", content, "object content wrong (3)");
        }
예제 #6
0
        public void testDownloadHelperPath()
        {

            string dir = rand8char();
            string file = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);

            // Create an object with content.
            ObjectId id = this.esu.CreateObjectOnPath( op, null, null, Encoding.UTF8.GetBytes("Four score and twenty years ago"), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);

            // Download the content
            DownloadHelper downloadHelper = new DownloadHelper(this.esu, null);
            MemoryStream ms = new MemoryStream();
            downloadHelper.ReadObject(op, ms, false);
            ms.Close();

            // Check the download

            string data = Encoding.UTF8.GetString(ms.ToArray());
            Assert.AreEqual(data, "Four score and twenty years ago", "object content wrong");

            // Download again 1 byte in a request
            downloadHelper = new DownloadHelper(this.esu, new byte[1]);
            ms = new MemoryStream();
            downloadHelper.ReadObject(op, ms, false);
            ms.Close();

            // Check the download
            data = Encoding.UTF8.GetString(ms.ToArray());
            Assert.AreEqual("Four score and twenty years ago", data, "object content wrong");
        }
예제 #7
0
 /// <summary>
 /// Creates a new object on the server with the contents of the given file,
 /// acl and metadata.
 /// </summary>
 /// <param name="path">The ObjectPath to create the new object on.</param>
 /// <param name="file">the path to the file to upload</param>
 /// <param name="acl">the ACL to assign to the new object.  Optional.  If null,
 /// the server will generate a default ACL for the file.</param>
 /// <param name="meta">The metadata to assign to the new object.
 /// Optional.  If null, no user metadata will be assigned to the new object.</param>
 /// <returns>the identifier of the newly-created object.</returns>
 public ObjectId CreateObjectOnPath(ObjectPath path, string file, Acl acl, MetadataList meta)
 {
     Stream s;
     // Open the file and call the streaming version
     try
     {
         totalBytes = new FileInfo(file).Length;
         s = File.OpenRead(file);
     }
     catch (FileNotFoundException e)
     {
         throw new EsuException("Could not open input file", e);
     }
     return CreateObjectOnPath(path, s, acl, meta, true);
 }
예제 #8
0
        public void testUtf8Content() {
            String oneByteCharacters = "Hello! ,";
            String twoByteCharacters = "\u0410\u0411\u0412\u0413"; // Cyrillic letters
            String fourByteCharacters = "\ud841\udf0e\ud841\udf31\ud841\udf79\ud843\udc53"; // Chinese symbols
            byte[] content = Encoding.UTF8.GetBytes(oneByteCharacters + twoByteCharacters + fourByteCharacters);
            ObjectPath path = new ObjectPath( TESTDIR + "utf8Content.txt" );

            // create object with multi-byte UTF-8 content
            this.esu.CreateObjectOnPath( path, null, null, content, "text/plain" );

            // verify content
            Assert.IsTrue( content.SequenceEqual( this.esu.ReadObject( path, null, null ) ), "content does not match" );
        }
예제 #9
0
	    public void testPathNaming() {
		    ObjectPath path = new ObjectPath( "/some/file" );
            Assert.IsFalse(path.IsDirectory(), "File should not be directory");
		    path = new ObjectPath( "/some/file.txt" );
            Assert.IsFalse(path.IsDirectory(), "File should not be directory");
            ObjectPath path2 = new ObjectPath("/some/file.txt");
		    Assert.AreEqual( path, path2, "Equal paths should be equal" );
    		
		    path = new ObjectPath( "/some/file/with/long.path/extra.stuff.here.zip" );
            Assert.IsFalse(path.IsDirectory(), "File should not be directory");
    		
		    path = new ObjectPath( "/" );
            Assert.IsTrue(path.IsDirectory(), "File should be directory");
    		
		    path = new ObjectPath( "/long/path/with/lots/of/elements/" );
            Assert.IsTrue(path.IsDirectory(), "File should be directory");
    		
	    }
예제 #10
0
	    private bool directoryContains( List<DirectoryEntry> dir, ObjectPath path ) {
		    foreach( DirectoryEntry de in dir ) {
			    if( de.Path.Equals( path ) ) {
				    return true;
			    }
		    }
    		
		    return false;
	    }
예제 #11
0
 private DirectoryEntry findInDirectory(List<DirectoryEntry> dir, ObjectPath path)
 {
     foreach (DirectoryEntry de in dir)
     {
         if (de.Path.Equals(path))
         {
             return de;
         }
     }
     return null;
 }
예제 #12
0
        public void testListDirectoryWithMetadata()
        {
            // Create an object
            MetadataList mlist = new MetadataList();
            Metadata listable = new Metadata("listable", "foo", true);
            Metadata unlistable = new Metadata("unlistable", "bar", false);
            Metadata listable2 = new Metadata("listable2", "foo2 foo2", true);
            Metadata unlistable2 = new Metadata("unlistable2", "bar2 bar2", false);
            mlist.AddMetadata(listable);
            mlist.AddMetadata(unlistable);
            mlist.AddMetadata(listable2);
            mlist.AddMetadata(unlistable2);
            string dir = rand8char();
            string file = rand8char();
            ObjectPath dirPath = new ObjectPath("/" + dir + "/");
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);

            ObjectId dirId = this.esu.CreateObjectOnPath(dirPath, null, null, null, null);
            ObjectId id = this.esu.CreateObjectOnPath(op, null, mlist, null, null);
            cleanup.Add(op);
            cleanup.Add(dirPath);
            Debug.WriteLine("Path: " + op + " ID: " + id);
            Assert.IsNotNull(id);
            Assert.IsNotNull(dirId);

            // Read back the content
            string content = Encoding.UTF8.GetString(this.esu.ReadObject(op, null, null));
            Assert.AreEqual("", content, "object content wrong");
            content = Encoding.UTF8.GetString(this.esu.ReadObject(id, null, null));
            Assert.AreEqual("", content, "object content wrong when reading by id");

            // List the parent path
            ListOptions options = new ListOptions();
            options.IncludeMetadata = true;
            //options.UserMetadata = new List<string>();
            //options.UserMetadata.Add("listable");
            List<DirectoryEntry> dirList = esu.ListDirectory(dirPath, options);
            Debug.WriteLine("Dir content: " + content);
            DirectoryEntry ent = findInDirectory(dirList, op);
            Assert.IsNotNull(ent, "File not found in directory");

            // Check metadata
            Assert.IsNotNull(ent.SystemMetadata.GetMetadata("size"), "Size missing from metadata");
            Assert.AreEqual("foo", ent.UserMetadata.GetMetadata("listable").Value, "Metadata value wrong");

            // listable2 should not be present
            //Assert.IsNull(ent.UserMetadata.GetMetadata("listable2"), "listable2 shouldn't be present");
        }
예제 #13
0
        public void testListDirectoryPaged()
        {
            string dir = rand8char();
            string file = rand8char();
            string file2 = rand8char();
            string dir2 = rand8char();
            ObjectPath dirPath = new ObjectPath("/" + dir + "/");
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);
            ObjectPath dirPath2 = new ObjectPath("/" + dir + "/" + dir2 + "/");
            ObjectPath op2 = new ObjectPath("/" + dir + "/" + file2);

            ObjectId dirId = this.esu.CreateObjectOnPath(dirPath, null, null, null, null);
            ObjectId id = this.esu.CreateObjectOnPath(op, null, null, null, null);
            this.esu.CreateObjectOnPath(dirPath2, null, null, null, null);
            this.esu.CreateObjectOnPath(op2, null, null, null, null);
            cleanup.Add(op);
            cleanup.Add(op2);
            cleanup.Add(dirPath2);
            cleanup.Add(dirPath);

            Debug.WriteLine("Path: " + op + " ID: " + id);
            Assert.IsNotNull(id);
            Assert.IsNotNull(dirId);

            // List the parent path
            ListOptions options = new ListOptions();
            options.IncludeMetadata = false;
            options.Limit = 1;
            List<DirectoryEntry> dirList = esu.ListDirectory(dirPath, options);
            // Iterate over token
            while (options.Token != null)
            {
                Debug.WriteLine("Continuing with token " + options.Token);
                dirList.AddRange(esu.ListDirectory(dirPath, options));
            }
            Assert.IsTrue(directoryContains(dirList, op), "File not found in directory");
            Assert.IsTrue(directoryContains(dirList, op2), "File2 not found in directory");
        }
예제 #14
0
       	public void testListDirectory() {
		    string dir = rand8char();
		    string file = rand8char();
		    string dir2 = rand8char();
            ObjectPath dirPath = new ObjectPath( "/" + dir + "/" );
    	    ObjectPath op = new ObjectPath( "/" + dir + "/" + file );
    	    ObjectPath dirPath2 = new ObjectPath( "/" + dir + "/" + dir2 + "/" );
        	
    	    ObjectId dirId = this.esu.CreateObjectOnPath( dirPath, null, null, null, null );
            ObjectId id = this.esu.CreateObjectOnPath( op, null, null, null, null );
            this.esu.CreateObjectOnPath( dirPath2, null, null, null, null );
            cleanup.Add( op );
            cleanup.Add( dirPath2 );
            cleanup.Add( dirPath );
            Debug.WriteLine( "Path: " + op + " ID: " + id );
            Assert.IsNotNull( id );
            Assert.IsNotNull(dirId);

            // Read back the content
            string content = Encoding.UTF8.GetString( this.esu.ReadObject( op, null, null ) );
            Assert.AreEqual(  "", content, "object content wrong" );
            content = Encoding.UTF8.GetString( this.esu.ReadObject( id, null, null ) );
            Assert.AreEqual("", content, "object content wrong when reading by id");
            
            // List the parent path
            ListOptions options = new ListOptions();
            options.IncludeMetadata = false;
            List<DirectoryEntry> dirList = esu.ListDirectory( dirPath, options );
            Debug.WriteLine( "Dir content: " + content );
            Assert.IsTrue( directoryContains( dirList, op ), "File not found in directory" );
            Assert.IsTrue( directoryContains( dirList, dirPath2 ), "subdirectory not found in directory" );
	    }
예제 #15
0
        public void testUnicodePath() {
            String dirName = rand8char();
            ObjectPath path = new ObjectPath( "/" + dirName + "/бöシ.txt" );
            ObjectId id = this.esu.CreateObjectOnPath( path, null, null, null, null );
            Assert.IsNotNull( id, "null ID returned" );
            cleanup.Add( id );

            ObjectPath parent = new ObjectPath( "/" + dirName + "/" );
            List<DirectoryEntry> ents = this.esu.ListDirectory( parent, null );
            bool found = false;
            foreach ( DirectoryEntry ent in ents ) {
                if ( ent.Path.Equals( path ) ) {
                    found = true;
                }
            }
            Assert.IsTrue( found, "Did not find unicode file in dir" );

            // Check read
            this.esu.ReadObject( path, null, null );

        }
예제 #16
0
        public void testUtf8Path() {
            String oneByteCharacters = "Hello! ,";
            String twoByteCharacters = "\u0410\u0411\u0412\u0413"; // Cyrillic letters
            String fourByteCharacters = "\ud841\udf0e\ud841\udf31\ud841\udf79\ud843\udc53"; // Chinese symbols
            String crazyName = oneByteCharacters + twoByteCharacters + fourByteCharacters;
            byte[] content = Encoding.UTF8.GetBytes( "Crazy name creation test." );
            ObjectPath path = new ObjectPath( TESTDIR + crazyName );

            // create crazy-name object
            this.esu.CreateObjectOnPath( path, null, null, content, "text/plain" );

            cleanup.Add(path);

            // verify name in directory list
            bool found = false;
            foreach ( DirectoryEntry entry in this.esu.ListDirectory( new ObjectPath( TESTDIR ), null ) ) {
                if ( entry.Path.ToString().Equals( path.ToString() ) ) {
                    found = true;
                    break;
                }
            }
            Assert.IsTrue( found, "crazyName not found in directory listing" );

            // verify content
            Assert.IsTrue( content.SequenceEqual( this.esu.ReadObject( path, null, null ) ), "content does not match" );
        }
예제 #17
0
	    public void testGetAllMetadataByPath() {
    	    ObjectPath op = new ObjectPath( "/" + rand8char() + ".tmp" );
            // Create an object with an ACL
            Acl acl = new Acl();
            acl.AddGrant(new Grant(new Grantee(esu.GetUid(), Grantee.GRANTEE_TYPE.USER), Permission.FULL_CONTROL));
            acl.AddGrant( new Grant( Grantee.OTHER, Permission.READ ) );
            MetadataList mlist = new MetadataList();
            Metadata listable = new Metadata( "listable", "foo", true );
            Metadata unlistable = new Metadata( "unlistable", "bar", false );
            Metadata listable2 = new Metadata( "listable2", "foo2 foo2", true );
            Metadata unlistable2 = new Metadata( "unlistable2", "bar2 bar2", false );
            mlist.AddMetadata( listable );
            mlist.AddMetadata( unlistable );
            mlist.AddMetadata( listable2 );
            mlist.AddMetadata( unlistable2 );

            ObjectId id = this.esu.CreateObjectOnPath( op, acl, null, null, null );
            this.esu.UpdateObject( op, null, mlist, null, null, null );
            Assert.IsNotNull( id, "null ID returned" );
            cleanup.Add( op );
            
            // Read it back with HEAD call
            ObjectMetadata om = this.esu.GetAllMetadata( op );
            Assert.IsNotNull( om.Metadata.GetMetadata( "listable" ), "value of 'listable' missing" );
            Assert.IsNotNull( om.Metadata.GetMetadata("unlistable"), "value of 'unlistable' missing" );
            Assert.IsNotNull( om.Metadata.GetMetadata("atime"), "value of 'atime' missing" );
            Assert.IsNotNull( om.Metadata.GetMetadata("ctime"), "value of 'ctime' missing" );
            Assert.AreEqual( "foo", om.Metadata.GetMetadata("listable").Value, "value of 'listable' wrong" );
            Assert.AreEqual( "bar", om.Metadata.GetMetadata("unlistable").Value, "value of 'unlistable' wrong" );

            // Check the ACL
            // not checking this by path because an extra groupid is added 
            // during the create calls by path.
            //Assert.AreEqual( acl, om.getAcl(), "ACLs don't match" );

	    }
예제 #18
0
        public void testGetShareableUrlOnPath()
        {
            // Create an object with content.
            string str = "Four score and twenty years ago";
            ObjectPath op = new ObjectPath("/" + rand8char() + ".txt");
            ObjectId id = this.esu.CreateObjectOnPath(op, null, null, Encoding.UTF8.GetBytes(str), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(op);

            DateTime expiration = DateTime.UtcNow;
            expiration += TimeSpan.FromHours(5);
            string disposition = "attachment; filename=\"no UTF support.txt\"; filename*=UTF-8''" + Uri.EscapeDataString("бöシ.txt");
            Uri u = esu.GetShareableUrl(id, expiration, disposition);

            Debug.WriteLine("Sharable URL: " + u);

            WebRequest wr = WebRequest.Create(u);
            WebResponse resp = wr.GetResponse();
            Stream s = resp.GetResponseStream();
            StreamReader sr = new StreamReader(s);
            string content = sr.ReadToEnd();
            sr.Close();
            Debug.WriteLine("Content: " + content);
            Assert.AreEqual(disposition, resp.Headers["Content-Disposition"]);
            Assert.AreEqual(str, content, "URL does not contain proper content");
        }
예제 #19
0
        public void testCreateObjectFromStreamOnPath()
        {
            string dir = rand8char();
            string file = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("hello"));

            ObjectId id = this.esu.CreateObjectFromStreamOnPath(op, null, null, ms, 5, "text/plain", null);
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);

            // Read back the content
            string content = Encoding.UTF8.GetString(this.esu.ReadObject(id, null, null));
            Assert.AreEqual("hello", content, "object content wrong");
        }
예제 #20
0
        /// <summary>
        /// Creates a new object on the server with the contents of the given stream,
        /// acl and metadata.
        /// </summary>
        /// <param name="path">The ObjectPath to create the object on.</param>
        /// <param name="stream">the stream to upload.  The stream will be read until
        /// an EOF is encountered.</param>
        /// <param name="acl">the ACL to assign to the new object.  Optional.  If null,
        /// the server will generate a default ACL for the file.</param>
        /// <param name="metadata">The metadata to assign to the new object.
        /// Optional.  If null, no user metadata will be assigned to the new object.</param>
        /// <param name="closeStream">if true, the stream will be closed after
        /// the transfer completes.  If false, the stream will not be closed.</param>
        /// <returns>the identifier of the newly-created object.</returns>
        public ObjectId CreateObjectOnPath(ObjectPath path, Stream stream, Acl acl,
                MetadataList metadata, bool closeStream)
        {

            this.currentBytes = 0;
            this.complete = false;
            this.failed = false;
            this.error = null;
            this.closeStream = closeStream;
            this.stream = stream;

            if (computeChecksums)
            {
                checksum = new Checksum(Checksum.Algorithm.SHA0);
            }
            else
            {
                checksum = null;
            }

            ObjectId id = null;

            // First call should be to create object
            try
            {
                bool eof = ReadChunk();
                id = this.esu.CreateObjectFromSegmentOnPath(path, acl, metadata, buffer, contentType, checksum);
                if (!eof)
                {
                    this.OnProgress(buffer.Count);
                }
                else
                {
                    // No data in file? Complete
                    this.OnComplete();
                    return id;
                }

                // Continue appending
                this.AppendChunks(path);

            }
            catch (EsuException e)
            {
                this.OnFail(e);
                throw e;
            }
            catch (IOException e)
            {
                this.OnFail(e);
                throw new EsuException("Error uploading object", e);
            }

            return id;
        }
예제 #21
0
        public void testUpdateHelperPath()
        {
            string dir = rand8char();
            string file = rand8char();
            ObjectPath op = new ObjectPath("/" + dir + "/" + file);

            // use a blocksize of 1 to test multiple transfers.
            UploadHelper uploadHelper = new UploadHelper(this.esu, new byte[1]);
            uploadHelper.ContentType = "text/plain";

            // Create an object with content.
            ObjectId id = this.esu.CreateObjectOnPath(op, null, null, Encoding.UTF8.GetBytes("Four score and twenty years ago"), "text/plain");
            Assert.IsNotNull(id, "null ID returned");
            cleanup.Add(id);

            // update the object contents
            MemoryStream ms = new MemoryStream();
            ms.Write(Encoding.UTF8.GetBytes("hello"), 0, 5);
            ms.Seek(0, SeekOrigin.Begin);

            uploadHelper.UpdateObject(op,
                    ms, null, null, true);

            // Read contents back and check them
            string content = Encoding.UTF8.GetString(this.esu.ReadObject(op, null, null));
            Assert.AreEqual("hello", content, "object content wrong");
        }