OSS中Object的元数据。

包含了用户自定义的元数据,也包含了OSS发送的标准HTTP头(如Content-Length, ETag等)。

        public static void ClassInitialize()
        {
            //get a OSS client object
            _ossClient = OssClientFactory.CreateOssClient();
            //get current class name, which is prefix of bucket/object
            _className = TestContext.CurrentContext.Test.FullName;
            _className = _className.Substring(_className.LastIndexOf('.') + 1).ToLowerInvariant();
            //create the bucket
            _bucketName = OssTestUtils.GetBucketName(_className);
            _ossClient.CreateBucket(_bucketName);

            //upload sample object as source object
            _sourceObjectKey = OssTestUtils.GetObjectKey(_className);
            var metadata = new ObjectMetadata();
            var poResult = OssTestUtils.UploadObject(_ossClient, _bucketName, _sourceObjectKey, 
                Config.UploadSampleFile, metadata);
            _sourceObjectETag = poResult.ETag;

            //upload multipart sample object as source object
            _sourceBigObjectKey = _sourceObjectKey + ".js";
            metadata = new ObjectMetadata();
            poResult = OssTestUtils.UploadObject(_ossClient, _bucketName, _sourceBigObjectKey,
                Config.MultiUploadSampleFile, metadata);
            _sourceBigObjectETag = poResult.ETag;
        }
 /// <summary>
 /// 构造一个新的<see cref="InitiateMultipartUploadRequest" />实例。
 /// </summary>
 /// <param name="bucketName"><see cref="OssObject" />所在<see cref="Bucket" />的名称。</param>
 /// <param name="key"><see cref="OssObject" />的<see cref="P:OssObject.Key" />。</param>
 /// <param name="objectMetadata">文件的元数据. <see cref="ObjectMetadata"/></param>
 public InitiateMultipartUploadRequest(string bucketName, string key, 
     ObjectMetadata objectMetadata)
 {
     BucketName = bucketName;
     Key = key;
     ObjectMetadata = objectMetadata;
     EncodingType = Util.HttpUtils.UrlEncodingType;
 }
Example #3
0
        public static void PutObject()
        {
            key =DateTime.Now.ToShortDateString()+"/aaa.jpg";
            try
            {
                var credentials = GetSecurityToken();
                var ossClient = new OssClient(endpoint, credentials.AccessKeyId, credentials.AccessKeySecret, credentials.SecurityToken);

                // 1. put object to specified output stream
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var metadata = new ObjectMetadata();
                    //metadata.UserMetadata.Add("mykey1", "myval1");
                    //metadata.UserMetadata.Add("mykey2", "myval2");
                    metadata.CacheControl = "No-Cache";
                    metadata.ContentType = "image/jpeg";
                    metadata.ContentLength = fs.Length;
                    ossClient.PutObject(bucketName, key, fs);
                }

                 //2. put object to specified file
                //client.PutObject(bucketName, key, fileToUpload);

                // 3. put object from specified object with multi-level virtual directory
                //key = "folder/sub_folder/key0";
                //client.PutObject(bucketName, key, fileToUpload);

            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public void CopyObjectBasicTest()
        {
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            //construct metadata
            var metadata = new ObjectMetadata();
            const string userMetaKey = "myKey";
            const string userMetaValue = "myValue";
            metadata.UserMetadata.Add(userMetaKey, userMetaValue);
            metadata.CacheControl = "No-Cache";

            var coRequest = new CopyObjectRequest(_bucketName, _sourceObjectKey, _bucketName, targetObjectKey)
            {
                NewObjectMetadata = metadata
            };

            //copy object
            _ossClient.CopyObject(coRequest);
            Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, targetObjectKey));
            var resMetadata = _ossClient.GetObjectMetadata(_bucketName, targetObjectKey);
            Assert.AreEqual(userMetaValue, resMetadata.UserMetadata[userMetaKey]);

            _ossClient.DeleteObject(_bucketName, targetObjectKey);
        }
 public static PutObjectResult UploadObject(IOss ossClient, string bucketName, 
     string objectKeyName, string originalFile, ObjectMetadata metadata)
 {
     //using (var fs = File.Open(originalFile, FileMode.Open))
     //{
     //    return ossClient.PutObject(bucketName, objectKeyName, fs, metadata);
     //}
     return ossClient.PutObject(bucketName, objectKeyName, originalFile, metadata);
 }
Example #6
0
        public static bool Upload(string bucketName, string fileName, string fileToUpload)
        {
            bool success = false;
            try
            {
                if (!DoesBucketExist(bucketName))
                {
                    if (!CreateBucket(bucketName))
                    {
                        return success;
                    }
                }

                string eTag;
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    eTag = OssUtils.ComputeContentMd5(fs, fs.Length);
                }

                var objectMeta = new ObjectMetadata { ETag = eTag };
                var result = ossClient.PutObject(bucketName, fileName, fileToUpload, objectMeta);
                success = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
            }
            return success;
        }
        public void CopyObjectBasicTestWithInvalidBucket()
        {
            var targetObjectKey = OssTestUtils.GetObjectKey(_className);

            //construct metadata
            var metadata = new ObjectMetadata();
            const string userMetaKey = "myKey";
            const string userMetaValue = "myValue";
            metadata.UserMetadata.Add(userMetaKey, userMetaValue);
            metadata.CacheControl = "No-Cache";

            try
            {
                var coRequest = new CopyObjectRequest(_bucketName, _sourceObjectKey, "/invalid_bucket", targetObjectKey)
                {
                    NewObjectMetadata = metadata
                };

                _ossClient.CopyObject(coRequest);

                Assert.Fail("Copy object should not pass with invalid bucket name");
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }
        }
 /// <summary>
 /// Creates a new instance of <see cref="PutObjectRequest" />
 /// </summary>
 /// <param name="bucketName">bucket name</param>
 /// <param name="key">object key</param>
 /// <param name="content">content to upload</param>
 /// <param name="metadata">metadata to set</param>
 public PutObjectRequest(string bucketName, string key, Stream content, ObjectMetadata metadata)
     : this(bucketName, key, content, metadata, false)
 {
 }
 public CreateSymlinkRequest(string bucketName, string symlink, string target, ObjectMetadata metadata)
     : this(bucketName, symlink, target)
 {
     ObjectMetadata = metadata;
 }
        public void UploadObjectFullSettingsTest()
        {
            //test folder structure
            var folder = OssTestUtils.GetObjectKey(_className);
            var key = folder + "/" + OssTestUtils.GetObjectKey(_className);

            //config metadata
            var metadata = new ObjectMetadata
            {
                CacheControl = "no-cache",
                ContentDisposition = "abc.zip",
                ContentEncoding = "gzip"
            };
            var eTag = FileUtils.ComputeContentMd5(Config.UploadSampleFile);
            metadata.ETag = eTag;
            //enable server side encryption
            const string encryption = "AES256";
            metadata.ServerSideEncryption = encryption;
            //user metadata
            metadata.UserMetadata.Add("MyKey1", "MyValue1");
            metadata.UserMetadata.Add("MyKey2", "MyValue2");

            try
            {
                //upload object
                OssTestUtils.UploadObject(_ossClient, _bucketName, key,
                    Config.UploadSampleFile, metadata);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var uploadedObjectMetadata = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(eTag.ToLowerInvariant(), uploadedObjectMetadata.ETag.ToLowerInvariant());
                Assert.AreEqual(encryption, uploadedObjectMetadata.ServerSideEncryption);
                Assert.AreEqual(2, uploadedObjectMetadata.UserMetadata.Count);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void ModifyObjectMetaWithToEmpty()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                var meta = new ObjectMetadata()
                {
                    ContentType = "text/rtf",
                };

                _ossClient.PutObject(_bucketName, key, Config.UploadSampleFile, meta);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var oldMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(meta.ContentType, oldMeta.ContentType);
                Assert.AreEqual(meta.CacheControl, oldMeta.CacheControl);

                _ossClient.ModifyObjectMeta(_bucketName, key, new ObjectMetadata());

                var newMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual("application/octet-stream", newMeta.ContentType);
                Assert.AreEqual(null, newMeta.CacheControl);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void ModifyObjectMetaWithA2CB()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                var meta = new ObjectMetadata()
                {
                    ContentType = "text/rtf",
                };

                _ossClient.PutObject(_bucketName, key, Config.UploadSampleFile, meta);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var oldMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(meta.ContentType, oldMeta.ContentType);
                Assert.AreEqual(meta.CacheControl, oldMeta.CacheControl);

                var toModifyMeta = new ObjectMetadata()
                {
                    ContentType = "application/vnd.wap.wmlc",
                    CacheControl = "max-stale"
                };
                _ossClient.ModifyObjectMeta(_bucketName, key, toModifyMeta);

                var newMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual("application/vnd.wap.wmlc", newMeta.ContentType);
                Assert.AreEqual("max-stale", newMeta.CacheControl);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void ModifyObjectMetaWithA2B()
        {
            var key = OssTestUtils.GetObjectKey(_className);

            try
            {
                var meta = new ObjectMetadata()
                {
                    ContentType = "text/rtf"
                };

                _ossClient.PutObject(_bucketName, key, Config.UploadSampleFile, meta);
                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var oldMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(meta.ContentType, oldMeta.ContentType);

                meta.ContentType = "application/mac-binhex40";
                _ossClient.ModifyObjectMeta(_bucketName, key, meta);

                var newMeta = _ossClient.GetObjectMetadata(_bucketName, key);
                Assert.AreEqual(meta.ContentType, newMeta.ContentType);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }
        public void UploadObjectWithKeySurfixAndMetadata()
        {
            var key = OssTestUtils.GetObjectKey(_className) + ".js";
            try
            {
                var metadata = new ObjectMetadata();
                metadata.ContentType = "application/vnd.android.package-archive";

                using (var fs = File.Open(Config.UploadSampleFile, FileMode.Open))
                {
                    _ossClient.PutObject(_bucketName, key, fs, metadata);
                }

                Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, key));

                var coRequest = new GetObjectRequest(_bucketName, key);
                var result = _ossClient.GetObject(coRequest);
                Assert.AreEqual("application/vnd.android.package-archive", result.Metadata.ContentType);
            }
            finally
            {
                if (OssTestUtils.ObjectExists(_ossClient, _bucketName, key))
                {
                    _ossClient.DeleteObject(_bucketName, key);
                }
            }
        }