public static void AsyncCopyObject(string sourceBucket, string sourceKey, string targetBucket, string targetKey)
        {
            try
            {
                var metadata = new ObjectMetadata();
                metadata.AddHeader("mk1", "mv1");
                metadata.AddHeader("mk2", "mv2");
                var req = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey)
                {
                    NewObjectMetadata = metadata
                };
                client.BeginCopyObject(req, CopyObjectCallback, null);

                _event.WaitOne();
            }
            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);
            }
        }
예제 #2
0
        /// <summary>
        /// sample for put object to oss
        /// </summary>
        public static void PutObject()
        {
            try
            {
                // 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 = "text/html";
                    metadata.ContentLength = fs.Length;
                    client.PutObject(bucketName, key, fs, metadata);
                }

                // 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 static void ModifyObjectMeta(string bucketName)
        {
            const string key = "key1";
            try
            {
                byte[] binaryData = Encoding.ASCII.GetBytes("forked from aliyun/aliyun-oss-csharp-sdk "); 
                var stream = new MemoryStream(binaryData);

                client.PutObject(bucketName, key, stream);

                var meta = new ObjectMetadata()
                {
                    ContentType = "application/msword"
                };

                client.ModifyObjectMeta(bucketName, key, meta);

                Console.WriteLine("Modify object meta succeeded");
            }
            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);
            }
        }
예제 #4
0
        public static void AsyncPutObject()
        {
            try
            {
                // 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 = "text/html";
                    client.BeginPutObject(bucketName, key, fs, metadata, PutObjectCallback, new string('a', 8));

                    _event.WaitOne();
                }
            }
            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);
            }
        }
예제 #5
0
        public void TestCreation()
        {
            const string DBPath = "test1.db";
            if (File.Exists(DBPath))
                File.Delete(DBPath);

            using (var provider = new QueryMetadataProvider())
            {
                const string TestKey = "TestKey";
                const string TestUrl = "a/test/url";
                const string TestValue = "test value";

                // Create database
                bool result = provider.Create(DBPath);
                Assert.True(result);
                // Add a key and retrieve it
                result = provider.AddKey(new MetadataKey(TestKey, MetadataKey.DatabaseType.String));
                Assert.True(result);
                IEnumerable<MetadataKey> fetchedKeys = provider.FetchAllKeys();
                Assert.NotNull(fetchedKeys);
                Assert.AreEqual(fetchedKeys.Count(), 1);
                MetadataKey key = fetchedKeys.First();
                Assert.NotNull(key);
                Assert.AreEqual(key.Name, TestKey);
                Assert.AreEqual(key.Type, MetadataKey.DatabaseType.String);

                // Add a metadata and retrieve it
                var stringMetadata = new ObjectMetadata<string>(TestUrl, key, TestValue);
                result = provider.Write(stringMetadata);
                Assert.True(result);

                IEnumerable<string> fetchedUrls = provider.FetchAllObjectUrls();
                Assert.NotNull(fetchedUrls);
                Assert.AreEqual(fetchedUrls.Count(), 1);
                string url = fetchedUrls.First();
                Assert.AreEqual(url, TestUrl);

                IEnumerable<IObjectMetadata> metadata = provider.Fetch(url);
                Assert.NotNull(metadata);
                Assert.AreEqual(metadata.Count(), 1);
                IObjectMetadata metadatum = metadata.First();
                Assert.AreEqual(metadatum.Key, key);
                Assert.AreEqual(metadatum.ObjectUrl, TestUrl);
                Assert.AreEqual(metadatum.Value, TestValue);

                provider.Close();
            }
        }
예제 #6
0
        public static void GetObject()
        {
            try
               {
                string eTag;
                using (Stream fs = File.Open(fileToUpload, FileMode.Open))
                {
                    // compute content's md5
                    eTag = ComputeContentMd5(fs);
                }

                // put object
                var metadata = new ObjectMetadata {ETag = eTag};
               ossClient.PutObject(bucketName, key, fileToUpload, metadata);

               Console.WriteLine("PutObject done.");

                // verify etag
                var o = ossClient.GetObject(bucketName, key);
                using (var requestStream = o.Content)
                {
                    int len = (int) o.Metadata.ContentLength;
                    var buf = new byte[len];
                    requestStream.Read(buf, 0, len);
                    var fs = File.Open(fileToDownload, FileMode.OpenOrCreate);
                    fs.Write(buf, 0, len);
                    //eTag = ComputeContentMd5(requestStream);
                    //Assert.AreEqual(o.Metadata.ETag, eTag.ToUpper());
                }

               Console.WriteLine("GetObject done.");
               }
               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);
               }
        }
예제 #7
0
 public static void CopyObject()
 {
     try
     {
         var metadata = new ObjectMetadata();
         metadata.AddHeader("mk1", "mv1");
         metadata.AddHeader("mk2", "mv2");
         var req = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey)
         {
             NewObjectMetadata = metadata
         };
         var ret = client.CopyObject(req);
         Console.WriteLine("target key's etag: " + ret.ETag);
     }
     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);
     }
 }
예제 #8
0
        private static object ReadValue(Type inst_type, JsonReader reader)
        {
            reader.Read();
            if (reader.Token == JsonToken.ArrayEnd)
            {
                return(null);
            }
            if (reader.Token == JsonToken.Null)
            {
                if (!inst_type.IsClass)
                {
                    throw new JsonException($"Can't assign null to an instance of type {inst_type}");
                }
                return(null);
            }
            if (reader.Token == JsonToken.Double || reader.Token == JsonToken.Int || reader.Token == JsonToken.Long || reader.Token == JsonToken.String || reader.Token == JsonToken.Boolean)
            {
                Type type = reader.Value.GetType();
                if (inst_type.IsAssignableFrom(type))
                {
                    return(reader.Value);
                }
                if (custom_importers_table.ContainsKey(type) && custom_importers_table[type].ContainsKey(inst_type))
                {
                    ImporterFunc importerFunc = custom_importers_table[type][inst_type];
                    return(importerFunc(reader.Value));
                }
                if (base_importers_table.ContainsKey(type) && base_importers_table[type].ContainsKey(inst_type))
                {
                    ImporterFunc importerFunc2 = base_importers_table[type][inst_type];
                    return(importerFunc2(reader.Value));
                }
                if (inst_type.IsEnum)
                {
                    return(Enum.ToObject(inst_type, reader.Value));
                }
                MethodInfo convOp = GetConvOp(inst_type, type);
                if (convOp != null)
                {
                    return(convOp.Invoke(null, new object[1]
                    {
                        reader.Value
                    }));
                }
                throw new JsonException($"Can't assign value '{reader.Value}' (type {type}) to type {inst_type}");
            }
            object obj = null;

            if (reader.Token == JsonToken.ArrayStart)
            {
                AddArrayMetadata(inst_type);
                ArrayMetadata arrayMetadata = array_metadata[inst_type];
                if (!arrayMetadata.IsArray && !arrayMetadata.IsList)
                {
                    throw new JsonException($"Type {inst_type} can't act as an array");
                }
                IList list;
                Type  elementType;
                if (!arrayMetadata.IsArray)
                {
                    list        = (IList)Activator.CreateInstance(inst_type);
                    elementType = arrayMetadata.ElementType;
                }
                else
                {
                    list        = new ArrayList();
                    elementType = inst_type.GetElementType();
                }
                while (true)
                {
                    object obj2 = ReadValue(elementType, reader);
                    if (obj2 == null && reader.Token == JsonToken.ArrayEnd)
                    {
                        break;
                    }
                    list.Add(obj2);
                }
                if (arrayMetadata.IsArray)
                {
                    int count = list.Count;
                    obj = Array.CreateInstance(elementType, count);
                    for (int i = 0; i < count; i++)
                    {
                        ((Array)obj).SetValue(list[i], i);
                    }
                }
                else
                {
                    obj = list;
                }
            }
            else if (reader.Token == JsonToken.ObjectStart)
            {
                AddObjectMetadata(inst_type);
                ObjectMetadata objectMetadata = object_metadata[inst_type];
                obj = Activator.CreateInstance(inst_type);
                while (true)
                {
                    reader.Read();
                    if (reader.Token == JsonToken.ObjectEnd)
                    {
                        break;
                    }
                    string text = (string)reader.Value;
                    if (objectMetadata.Properties.ContainsKey(text))
                    {
                        PropertyMetadata propertyMetadata = objectMetadata.Properties[text];
                        if (propertyMetadata.IsField)
                        {
                            ((FieldInfo)propertyMetadata.Info).SetValue(obj, ReadValue(propertyMetadata.Type, reader));
                            continue;
                        }
                        PropertyInfo propertyInfo = (PropertyInfo)propertyMetadata.Info;
                        if (propertyInfo.CanWrite)
                        {
                            propertyInfo.SetValue(obj, ReadValue(propertyMetadata.Type, reader), null);
                        }
                        else
                        {
                            ReadValue(propertyMetadata.Type, reader);
                        }
                    }
                    else if (!objectMetadata.IsDictionary)
                    {
                        if (!reader.SkipNonMembers)
                        {
                            throw new JsonException($"The type {inst_type} doesn't have the property '{text}'");
                        }
                        ReadSkip(reader);
                    }
                    else
                    {
                        ((IDictionary)obj).Add(text, ReadValue(objectMetadata.ElementType, reader));
                    }
                }
            }
            return(obj);
        }
예제 #9
0
 public void ProvisionIndex(ObjectMetadata metadata)
 {
     _indexer.ProvisionIndex(metadata);
 }
예제 #10
0
 public void BulkIndexObject(string objectFullName, ObjectMetadata metadata, IEnumerable<object[]> indexes)
 {
     lock (_bulkIndexLock)
     {
         _indexer.BulkUpsertIndexValues(objectFullName, metadata, indexes);
     }
 }
예제 #11
0
            private static void AddObjectMetadata(Type type)
            {
                if (object_metadata.ContainsKey(type))
                {
                    return;
                }

                ObjectMetadata data = new ObjectMetadata();

                if (type.GetInterface("System.Collections.IDictionary") != null)
                {
                    data.IsDictionary = true;
                }

                data.Properties = new Dictionary <string, PropertyMetadata>();

                foreach (PropertyInfo p_info in type.GetProperties())
                {
                    if (p_info.Name == "Item")
                    {
                        ParameterInfo[] parameters = p_info.GetIndexParameters();

                        if (parameters.Length != 1)
                        {
                            continue;
                        }

                        if (parameters[0].ParameterType == typeof(string))
                        {
                            data.ElementType = p_info.PropertyType;
                        }

                        continue;
                    }

                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info = p_info;
                    p_data.Type = p_info.PropertyType;

                    data.Properties.Add(p_info.Name, p_data);
                }

                foreach (FieldInfo f_info in type.GetFields())
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info    = f_info;
                    p_data.IsField = true;
                    p_data.Type    = f_info.FieldType;

                    data.Properties.Add(f_info.Name, p_data);
                }

                lock (object_metadata_lock)
                {
                    try
                    {
                        object_metadata.Add(type, data);
                    }
                    catch (ArgumentException)
                    {
                        return;
                    }
                }
            }
예제 #12
0
 public void WriteObjectMetadata(ObjectMetadata metadata)
 {
     _out.WriteLine(ObjectMetadataPrefix + BinaryHelper.ByteToHexString(SerializerHelper.Serialize<ObjectMetadata>(metadata)));
 }
예제 #13
0
        internal static async Task HttpPutObject(RequestMetadata md)
        {
            string header = md.Http.Request.SourceIp + ":" + md.Http.Request.SourcePort + " ";

            ContainerClient client = _ContainerMgr.GetContainerClient(md.Params.UserGuid, md.Params.ContainerName);

            if (client == null)
            {
                _Logging.Warn(header + "HttpPutObject unable to find container " + md.Params.UserGuid + "/" + md.Params.ContainerName);
                md.Http.Response.StatusCode  = 404;
                md.Http.Response.ContentType = "application/json";
                await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(5, 404, null, null), true));

                return;
            }

            if (!client.Container.IsPublicWrite)
            {
                if (md.User == null || !(md.User.GUID.ToLower().Equals(md.Params.UserGuid.ToLower())))
                {
                    _Logging.Warn(header + "HttpPutObject unauthorized unauthenticated write attempt to object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey);
                    md.Http.Response.StatusCode  = 401;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(3, 401, null, null), true));

                    return;
                }
            }

            if (md.Perm != null)
            {
                if (!md.Perm.WriteObject)
                {
                    _Logging.Warn(header + "HttpPutObject unauthorized write attempt to object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey);
                    md.Http.Response.StatusCode  = 401;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(3, 401, null, null), true));

                    return;
                }
            }

            if (!client.Exists(md.Params.ObjectKey))
            {
                _Logging.Warn(header + "HttpPutObject object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + " does not exists");
                md.Http.Response.StatusCode  = 404;
                md.Http.Response.ContentType = "application/json";
                await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(5, 404, null, null), true));

                return;
            }

            ErrorCode error;

            if (!String.IsNullOrEmpty(md.Params.Rename))
            {
                #region Rename

                if (!_ObjectHandler.Rename(md, client, md.Params.ObjectKey, md.Params.Rename, out error))
                {
                    _Logging.Warn(header + "HttpPutObject unable to rename object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + " to " + md.Params.Rename + ": " + error.ToString());

                    int statusCode = 0;
                    int id         = 0;
                    ContainerClient.HttpStatusFromErrorCode(error, out statusCode, out id);

                    md.Http.Response.StatusCode  = statusCode;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(id, statusCode, "Unable to rename.", error), true));

                    return;
                }
                else
                {
                    md.Http.Response.StatusCode = 200;
                    await md.Http.Response.Send();

                    return;
                }

                #endregion
            }
            else if (md.Params.Index != null)
            {
                #region Range-Write

                #region Verify-Transfer-Size

                if (md.Http.Request.ContentLength > _Settings.Server.MaxTransferSize ||
                    (md.Http.Request.Data != null && md.Http.Request.ContentLength > _Settings.Server.MaxTransferSize)
                    )
                {
                    _Logging.Warn(header + "HttpPutObject transfer size too large (count requested: " + md.Params.Count + ")");
                    md.Http.Response.StatusCode  = 413;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(11, 413, null, null), true));

                    return;
                }

                #endregion

                #region Retrieve-Original-Data

                long   originalContentLength = 0;
                Stream originalDataStream    = null;
                byte[] originalData          = null;
                string originalContentType   = null;

                if (!_ObjectHandler.Read(
                        md,
                        client,
                        md.Params.ObjectKey,
                        Convert.ToInt32(md.Params.Index),
                        Convert.ToInt32(md.Http.Request.ContentLength),
                        out originalContentType,
                        out originalContentLength,
                        out originalDataStream,
                        out error))
                {
                    if (error == ErrorCode.OutOfRange)
                    {
                        // continue, simply appending the data
                        originalData = new byte[md.Http.Request.ContentLength];
                        for (int i = 0; i < originalData.Length; i++)
                        {
                            originalData[i] = 0x00;
                        }
                    }
                    else
                    {
                        _Logging.Warn(header + "HttpPutObject unable to retrieve original data from object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + ": " + error.ToString());
                        md.Http.Response.StatusCode  = 500;
                        md.Http.Response.ContentType = "application/json";
                        await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(4, 500, "Unable to retrieve original data.", error), true));

                        return;
                    }
                }
                else
                {
                    if (originalContentLength > 0 && originalDataStream != null)
                    {
                        originalData = Common.StreamToBytes(originalDataStream);
                    }
                }

                #endregion

                #region Perform-Update

                if (!_ObjectHandler.WriteRange(md, client, md.Params.ObjectKey, Convert.ToInt64(md.Params.Index), md.Http.Request.ContentLength, md.Http.Request.Data, out error))
                {
                    _Logging.Warn(header + "HttpPutObject unable to write range to object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + ": " + error.ToString());

                    int statusCode = 0;
                    int id         = 0;
                    ContainerClient.HttpStatusFromErrorCode(error, out statusCode, out id);

                    md.Http.Response.StatusCode  = statusCode;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(id, statusCode, "Unable to write range to object.", error), true));

                    return;
                }
                else
                {
                    md.Http.Response.StatusCode = 200;
                    await md.Http.Response.Send();

                    return;
                }

                #endregion

                #endregion
            }
            else if (!String.IsNullOrEmpty(md.Params.Tags))
            {
                #region Tags

                #region Retrieve-Original-Object-Metadata

                ObjectMetadata originalMetadata = null;
                if (!client.ReadObjectMetadata(md.Params.ObjectKey, out originalMetadata))
                {
                    _Logging.Warn(header + "HttpPutObject unable to read original metadata for tag rewrite for object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey);
                    md.Http.Response.StatusCode  = 404;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(5, 404, null, null), true));

                    return;
                }

                #endregion

                #region Update-Tags

                if (!client.WriteObjectTags(md.Params.ObjectKey, md.Params.Tags, out error))
                {
                    _Logging.Warn(header + "HttpPutObject unable to write tags to object " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + ": " + error.ToString());

                    int statusCode = 0;
                    int id         = 0;
                    ContainerClient.HttpStatusFromErrorCode(error, out statusCode, out id);

                    md.Http.Response.StatusCode  = statusCode;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(id, statusCode, "Unable to write tags to object.", error), true));

                    return;
                }
                else
                {
                    md.Http.Response.StatusCode = 200;
                    await md.Http.Response.Send();

                    return;
                }

                #endregion

                #endregion
            }
            else if (md.Params.Keys)
            {
                #region Update-Keys

                Dictionary <string, string> keys = null;

                if (md.Http.Request.Data != null && md.Http.Request.ContentLength > 0)
                {
                    byte[] reqData = Common.StreamToBytes(md.Http.Request.Data);

                    try
                    {
                        keys = Common.DeserializeJson <Dictionary <string, string> >(reqData);
                    }
                    catch (Exception)
                    {
                        _Logging.Warn(header + "HttpPutContainer unable to deserialize request body");
                        md.Http.Response.StatusCode  = 400;
                        md.Http.Response.ContentType = "application/json";
                        await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(9, 400, null, null), true));

                        return;
                    }
                }

                if (!_ObjectHandler.WriteKeyValues(md, client, md.Params.ObjectKey, keys, out error))
                {
                    _Logging.Warn(header + "HttpPutObject unable to write keys to " + md.Params.UserGuid + "/" + md.Params.ContainerName + "/" + md.Params.ObjectKey + ": " + error.ToString());

                    int statusCode = 0;
                    int id         = 0;
                    ContainerClient.HttpStatusFromErrorCode(error, out statusCode, out id);

                    md.Http.Response.StatusCode  = statusCode;
                    md.Http.Response.ContentType = "application/json";
                    await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(id, statusCode, "Unable to write keys.", error), true));

                    return;
                }
                else
                {
                    md.Http.Response.StatusCode = 201;
                    await md.Http.Response.Send();

                    return;
                }

                #endregion
            }
            else
            {
                _Logging.Warn(header + "HttpPutObject request query does not contain _index, _rename, or _tags");
                md.Http.Response.StatusCode  = 400;
                md.Http.Response.ContentType = "application.json";
                await md.Http.Response.Send(Common.SerializeJson(new ErrorResponse(2, 400, "Querystring must contain values for '_index', '_rename', or '_tags'.", null), true));

                return;
            }
        }
예제 #14
0
 public static OcsObjectMetadata Map(ObjectMetadata objectMetadata)
 => new OcsObjectMetadata(objectMetadata.hash,
                          objectMetadata.last_modified,
                          objectMetadata.bytes,
                          objectMetadata.name,
                          objectMetadata.content_type);
예제 #15
0
            private static object ReadValue(Type inst_type, JsonReader reader)
            {
                reader.Read();

                if (reader.Token == JsonToken.ArrayEnd)
                {
                    return(null);
                }

                Type underlying_type = Nullable.GetUnderlyingType(inst_type);
                Type value_type      = underlying_type ?? inst_type;

                if (reader.Token == JsonToken.Null)
                {
                    if (inst_type.IsClass || underlying_type != null)
                    {
                        return(null);
                    }

                    throw new JsonException(String.Format(
                                                "Can't assign null to an instance of type {0}",
                                                inst_type));
                }

                if (reader.Token == JsonToken.Double ||
                    reader.Token == JsonToken.Int ||
                    reader.Token == JsonToken.Long ||
                    reader.Token == JsonToken.String ||
                    reader.Token == JsonToken.Boolean)
                {
                    Type json_type = reader.Value.GetType();

                    if (value_type.IsAssignableFrom(json_type))
                    {
                        return(reader.Value);
                    }

                    // If there's a custom importer that fits, use it
                    if (custom_importers_table.ContainsKey(json_type) &&
                        custom_importers_table[json_type].ContainsKey(
                            value_type))
                    {
                        ImporterFunc importer =
                            custom_importers_table[json_type][value_type];

                        return(importer(reader.Value));
                    }

                    // Maybe there's a base importer that works
                    if (base_importers_table.ContainsKey(json_type) &&
                        base_importers_table[json_type].ContainsKey(
                            value_type))
                    {
                        ImporterFunc importer =
                            base_importers_table[json_type][value_type];

                        return(importer(reader.Value));
                    }

                    // Maybe it's an enum
                    if (value_type.IsEnum)
                    {
                        return(Enum.ToObject(value_type, reader.Value));
                    }

                    // Try using an implicit conversion operator
                    MethodInfo conv_op = GetConvOp(value_type, json_type);

                    if (conv_op != null)
                    {
                        return(conv_op.Invoke(null,
                                              new object[] { reader.Value }));
                    }

                    // No luck
                    throw new JsonException(String.Format(
                                                "Can't assign value '{0}' (type {1}) to type {2}",
                                                reader.Value, json_type, inst_type));
                }

                object instance = null;

                if (reader.Token == JsonToken.ArrayStart)
                {
                    AddArrayMetadata(inst_type);
                    ArrayMetadata t_data = array_metadata[inst_type];

                    if (!t_data.IsArray && !t_data.IsList)
                    {
                        throw new JsonException(String.Format(
                                                    "Type {0} can't act as an array",
                                                    inst_type));
                    }

                    IList list;
                    Type  elem_type;

                    if (!t_data.IsArray)
                    {
                        list      = (IList)Activator.CreateInstance(inst_type);
                        elem_type = t_data.ElementType;
                    }
                    else
                    {
                        list      = new ArrayList();
                        elem_type = inst_type.GetElementType();
                    }

                    while (true)
                    {
                        object item = ReadValue(elem_type, reader);
                        if (item == null && reader.Token == JsonToken.ArrayEnd)
                        {
                            break;
                        }

                        list.Add(item);
                    }

                    if (t_data.IsArray)
                    {
                        int n = list.Count;
                        instance = Array.CreateInstance(elem_type, n);

                        for (int i = 0; i < n; i++)
                        {
                            ((Array)instance).SetValue(list[i], i);
                        }
                    }
                    else
                    {
                        instance = list;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    AddObjectMetadata(value_type);
                    ObjectMetadata t_data = object_metadata[value_type];

                    instance = Activator.CreateInstance(value_type);

                    while (true)
                    {
                        reader.Read();

                        if (reader.Token == JsonToken.ObjectEnd)
                        {
                            break;
                        }

                        string property = (string)reader.Value;

                        if (t_data.Properties.ContainsKey(property))
                        {
                            PropertyMetadata prop_data =
                                t_data.Properties[property];

                            if (prop_data.IsField)
                            {
                                ((FieldInfo)prop_data.Info).SetValue(
                                    instance, ReadValue(prop_data.Type, reader));
                            }
                            else
                            {
                                PropertyInfo p_info =
                                    (PropertyInfo)prop_data.Info;

                                if (p_info.CanWrite)
                                {
                                    p_info.SetValue(
                                        instance,
                                        ReadValue(prop_data.Type, reader),
                                        null);
                                }
                                else
                                {
                                    ReadValue(prop_data.Type, reader);
                                }
                            }
                        }
                        else
                        {
                            if (!t_data.IsDictionary)
                            {
                                if (!reader.SkipNonMembers)
                                {
                                    throw new JsonException(String.Format(
                                                                "The type {0} doesn't have the " +
                                                                "property '{1}'",
                                                                inst_type, property));
                                }
                                else
                                {
                                    ReadSkip(reader);
                                    continue;
                                }
                            }

                            ((IDictionary)instance).Add(
                                property, ReadValue(
                                    t_data.ElementType, reader));
                        }
                    }
                }

                return(instance);
            }
예제 #16
0
        /// <summary>
        /// Returns all of an object's metadata and its ACL in
        /// one call.
        /// </summary>
        /// <param name="id">the object's identifier.</param>
        /// <returns>the object's metadata</returns>
        public ObjectMetadata GetAllMetadata( Identifier id )
        {
            HttpWebResponse resp = null;
            try {
                string resource = getResourcePath(context, id);
                Uri u = buildUrl(resource);
                HttpWebRequest con = createWebRequest(u);

                // Build headers
                Dictionary<string, string> headers = new Dictionary<string, string>();

                headers.Add("x-emc-uid", uid);
                if (utf8Enabled) headers.Add("x-emc-utf8", "true");
                if (id is ObjectKey) {
                    headers.Add("x-emc-pool", (id as ObjectKey).pool);
                }

                // Add date
                addDateHeader(headers);

                // Sign request
                signRequest(con, "HEAD", resource, headers);

                // Check response
                resp = (HttpWebResponse)con.GetResponse();
                int statInt = (int)resp.StatusCode;
                if (statInt > 299) {
                    handleError(resp);
                }

                // Parse return headers.  Regular metadata is in x-emc-meta and
                // listable metadata is in x-emc-listable-meta
                MetadataList meta = new MetadataList();
                readMetadata(meta, resp.Headers["x-emc-meta"], false);
                readMetadata(meta, resp.Headers["x-emc-listable-meta"], true);

                // Parse return headers.  User grants are in x-emc-useracl and
                // group grants are in x-emc-groupacl
                Acl acl = new Acl();
                readAcl(acl, resp.Headers["x-emc-useracl"], Grantee.GRANTEE_TYPE.USER);
                readAcl(acl, resp.Headers["x-emc-groupacl"], Grantee.GRANTEE_TYPE.GROUP);

                ObjectMetadata om = new ObjectMetadata();
                om.ACL = acl;
                om.Metadata = meta;

                return om;

            } catch (UriFormatException e) {
                throw new EsuException("Invalid URL", e);
            } catch (IOException e) {
                throw new EsuException("Error connecting to server", e);
            } catch (WebException e) {
                if (e.Response != null)
                {
                    handleError((HttpWebResponse)e.Response);
                }
                else
                {
                    throw new EsuException("Error executing request: " + e.Message, e);
                }
            }
            finally
            {
                if (resp != null) {
                    resp.Close();
                }
            }
            return null;
        }
예제 #17
0
    // Token: 0x06015702 RID: 87810 RVA: 0x0056F278 File Offset: 0x0056D478
    private static object ReadValue(Type inst_type, JsonReader reader)
    {
        reader.Read();
        if (reader.Token == JsonToken.ArrayEnd)
        {
            return(null);
        }
        if (reader.Token == JsonToken.Null)
        {
            if (!inst_type.IsClass)
            {
                throw new JsonException(string.Format("Can't assign null to an instance of type {0}", inst_type));
            }
            return(null);
        }
        else
        {
            if (reader.Token != JsonToken.Double && reader.Token != JsonToken.Int && reader.Token != JsonToken.Long && reader.Token != JsonToken.String && reader.Token != JsonToken.Boolean)
            {
                object obj = null;
                if (reader.Token == JsonToken.ArrayStart)
                {
                    JsonMapper.AddArrayMetadata(inst_type);
                    ArrayMetadata arrayMetadata = JsonMapper.array_metadata[inst_type];
                    if (!arrayMetadata.IsArray && !arrayMetadata.IsList)
                    {
                        throw new JsonException(string.Format("Type {0} can't act as an array", inst_type));
                    }
                    IList list;
                    Type  elementType;
                    if (!arrayMetadata.IsArray)
                    {
                        list        = (IList)Activator.CreateInstance(inst_type);
                        elementType = arrayMetadata.ElementType;
                    }
                    else
                    {
                        list        = new ArrayList();
                        elementType = inst_type.GetElementType();
                    }
                    for (;;)
                    {
                        object value = JsonMapper.ReadValue(elementType, reader);
                        if (reader.Token == JsonToken.ArrayEnd)
                        {
                            break;
                        }
                        list.Add(value);
                    }
                    if (arrayMetadata.IsArray)
                    {
                        int count = list.Count;
                        obj = Array.CreateInstance(elementType, count);
                        for (int i = 0; i < count; i++)
                        {
                            ((Array)obj).SetValue(list[i], i);
                        }
                    }
                    else
                    {
                        obj = list;
                    }
                }
                else if (reader.Token == JsonToken.ObjectStart)
                {
                    JsonMapper.AddObjectMetadata(inst_type);
                    ObjectMetadata objectMetadata = JsonMapper.object_metadata[inst_type];
                    obj = Activator.CreateInstance(inst_type);
                    string text;
                    for (;;)
                    {
                        reader.Read();
                        if (reader.Token == JsonToken.ObjectEnd)
                        {
                            break;
                        }
                        text = (string)reader.Value;
                        if (objectMetadata.Properties.ContainsKey(text))
                        {
                            PropertyMetadata propertyMetadata = objectMetadata.Properties[text];
                            if (propertyMetadata.IsField)
                            {
                                ((FieldInfo)propertyMetadata.Info).SetValue(obj, JsonMapper.ReadValue(propertyMetadata.Type, reader));
                            }
                            else
                            {
                                PropertyInfo propertyInfo = (PropertyInfo)propertyMetadata.Info;
                                if (propertyInfo.CanWrite)
                                {
                                    propertyInfo.SetValue(obj, JsonMapper.ReadValue(propertyMetadata.Type, reader), null);
                                }
                                else
                                {
                                    JsonMapper.ReadValue(propertyMetadata.Type, reader);
                                }
                            }
                        }
                        else
                        {
                            if (!objectMetadata.IsDictionary)
                            {
                                goto Block_27;
                            }
                            ((IDictionary)obj).Add(text, JsonMapper.ReadValue(objectMetadata.ElementType, reader));
                        }
                    }
                    return(obj);

Block_27:
                    throw new JsonException(string.Format("The type {0} doesn't have the property '{1}'", inst_type, text));
                }
                return(obj);
            }
            Type type = reader.Value.GetType();
            if (inst_type.IsAssignableFrom(type))
            {
                return(reader.Value);
            }
            if (JsonMapper.custom_importers_table.ContainsKey(type) && JsonMapper.custom_importers_table[type].ContainsKey(inst_type))
            {
                ImporterFunc importerFunc = JsonMapper.custom_importers_table[type][inst_type];
                return(importerFunc(reader.Value));
            }
            if (JsonMapper.base_importers_table.ContainsKey(type) && JsonMapper.base_importers_table[type].ContainsKey(inst_type))
            {
                ImporterFunc importerFunc2 = JsonMapper.base_importers_table[type][inst_type];
                return(importerFunc2(reader.Value));
            }
            if (inst_type.IsEnum)
            {
                return(Enum.ToObject(inst_type, reader.Value));
            }
            MethodInfo convOp = JsonMapper.GetConvOp(inst_type, type);
            if (convOp != null)
            {
                return(convOp.Invoke(null, new object[]
                {
                    reader.Value
                }));
            }
            throw new JsonException(string.Format("Can't assign value '{0}' (type {1}) to type {2}", reader.Value, type, inst_type));
        }
    }
예제 #18
0
        private ObjectMetadata[] generateObjectMetadataForTag(string tag, bool isAnimated)
        {
            // Encode these in a json string and send it to the server
            SimObj[] simObjects = GameObject.FindObjectsOfType(typeof(SimObj)) as SimObj[];

            HashSet <SimObj> visibleObjectIds = new HashSet <SimObj>();

            foreach (SimObj so in VisibleSimObjs())
            {
                visibleObjectIds.Add(so);
            }
            int numObj = simObjects.Length;
            List <ObjectMetadata>       metadata          = new List <ObjectMetadata>();
            Dictionary <string, string> parentReceptacles = new Dictionary <string, string> ();

            for (int k = 0; k < numObj; k++)
            {
                ObjectMetadata meta   = new ObjectMetadata();
                SimObj         simObj = simObjects[k];
                if (this.excludeObject(simObj))
                {
                    continue;
                }
                GameObject o = simObj.gameObject;


                meta.name     = o.name;
                meta.position = o.transform.position;
                meta.rotation = o.transform.eulerAngles;

                meta.objectType = Enum.GetName(typeof(SimObjType), simObj.Type);
                meta.receptacle = simObj.IsReceptacle;
                meta.openable   = IsOpenable(simObj);
                if (meta.openable)
                {
                    meta.isopen = IsOpen(simObj);
                }
                meta.pickupable = IsPickupable(simObj);

                if (meta.receptacle)
                {
                    List <string> receptacleObjectIds = new List <string>();
                    foreach (SimObj cso in SimUtil.GetItemsFromReceptacle(simObj.Receptacle))
                    {
                        receptacleObjectIds.Add(cso.UniqueID);
                        parentReceptacles.Add(cso.UniqueID, simObj.UniqueID);
                    }

                    List <PivotSimObj> pivotSimObjs = new List <PivotSimObj>();
                    for (int i = 0; i < simObj.Receptacle.Pivots.Length; i++)
                    {
                        Transform t = simObj.Receptacle.Pivots[i];
                        if (t.childCount > 0)
                        {
                            SimObj      psimobj = t.GetChild(0).GetComponent <SimObj>();
                            PivotSimObj pso     = new PivotSimObj();
                            pso.objectId = psimobj.UniqueID;
                            pso.pivotId  = i;
                            pivotSimObjs.Add(pso);
                        }
                    }
                    meta.pivotSimObjs        = pivotSimObjs.ToArray();
                    meta.receptacleObjectIds = receptacleObjectIds.ToArray();
                    meta.receptacleCount     = simObj.Receptacle.Pivots.Length;
                }

                meta.objectId = simObj.UniqueID;


                meta.visible  = (visibleObjectIds.Contains(simObj));
                meta.distance = Vector3.Distance(transform.position, o.transform.position);
                Bounds bounds = simObj.Bounds;
                meta.bounds3D = new [] {
                    bounds.min.x,
                    bounds.min.y,
                    bounds.min.z,
                    bounds.max.x,
                    bounds.max.y,
                    bounds.max.z,
                };

                metadata.Add(meta);
            }


            foreach (ObjectMetadata meta in metadata)
            {
                if (parentReceptacles.ContainsKey(meta.objectId))
                {
                    meta.parentReceptacle = parentReceptacles [meta.objectId];
                }
            }

            return(metadata.ToArray());
        }
예제 #19
0
 /// <summary>
 /// Retrieve metadata for an object.
 /// </summary>
 /// <param name="name">Object name.</param>
 /// <param name="metadata">Object metadata.</param>
 /// <returns>True if successful.</returns>
 public abstract bool GetObjectMetadata(string name, out ObjectMetadata metadata);
예제 #20
0
        /// <summary>
        /// 将指定的字符串内容上传为文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="key"></param>
        /// <param name="content"></param>
        /// <param name="mimeType"></param>
        /// <returns></returns>
        public async Task <OssResult <PutObjectResult> > PutObjectAsync(BucketInfo bucket, string key, string content, string mimeType = "text/plain", ObjectMetadata meta = null, IDictionary <string, string> extraHeaders = null)
        {
            var file = new RequestContent()
            {
                ContentType   = RequestContentType.String,
                StringContent = content,
                MimeType      = mimeType,
                Metadata      = meta
            };

            return(await PutObjectAsync(bucket, key, file, extraHeaders));
        }
예제 #21
0
        /// <summary>
        /// 根据指定的文件名上传文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="key"></param>
        /// <param name="filePathName"></param>
        /// <returns></returns>
        public async Task <OssResult <PutObjectResult> > PutObjectByFileNameAsync(BucketInfo bucket, string key,
                                                                                  string filePathName, ObjectMetadata meta = null, IDictionary <string, string> extraHeaders = null)
        {
            using (var stream = File.OpenRead(filePathName))
            {
                var file = new RequestContent()
                {
                    ContentType   = RequestContentType.Stream,
                    StreamContent = stream,
                    MimeType      = MimeHelper.GetMime(filePathName),
                    Metadata      = meta
                };

                return(await PutObjectAsync(bucket, key, file, extraHeaders));
            }
        }
예제 #22
0
        public void TestDelete()
        {
            const string DBPath = "test3.db";
            if (File.Exists(DBPath))
                File.Delete(DBPath);

            using (var provider = new QueryMetadataProvider())
            {
                // Create database and fill it
                provider.Create(DBPath);

                foreach (MetadataKey key in keys)
                    provider.AddKey(key);

                for (int i = 0; i < 4; ++i)
                {
                    provider.Write(new ObjectMetadata(urls[0], keys[i], values1[i]));
                    provider.Write(new ObjectMetadata(urls[1], keys[i], values2[i]));
                }

                for (int i = 0; i < 4; ++i)
                {
                    bool result = provider.Delete(new ObjectMetadata(urls[0], keys[i], values1[i]));
                    Assert.True(result);
                    if (i != 3)
                    {
                        IObjectMetadata data = provider.Fetch(urls[1], keys[i]);
                        result = provider.Delete(data);
                        Assert.True(result);
                    }
                }

                IEnumerable<IObjectMetadata> metadata = provider.FetchAll();
                Assert.NotNull(metadata);
                Assert.AreEqual(metadata.Count(), 1);
                IObjectMetadata fetchedData = metadata.First();
                Assert.NotNull(fetchedData);
                Assert.AreEqual(fetchedData.Key, keys[3]);
                Assert.AreEqual(fetchedData.Value, values2[3]);
            }

            using (var provider = new QueryMetadataProvider())
            {
                provider.Open(DBPath, false);

                IObjectMetadata data = new ObjectMetadata(urls[1], keys[3]);
                bool result = provider.Delete(data);
                Assert.True(result);
                IEnumerable<IObjectMetadata> metadata = provider.FetchAll();
                Assert.NotNull(metadata);
                Assert.AreEqual(metadata.Count(), 0);
            }
        }
 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));
 }
예제 #24
0
			public MethodBodyTransformation(ObjectMetadata currentComponent)
			{
				_this = currentComponent;
			}
예제 #25
0
        public static void PutObjectWithMd5(string bucketName)
        {
            const string key = "PutObjectWithMd5";

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

            var meta = new ObjectMetadata() { ETag = eTag };
            try
            {
                client.PutObject(bucketName, key, fileToUpload, meta);

                Console.WriteLine("Put object:{0} succeeded", key);
            }
            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);
            }
        }
예제 #26
0
        public void MultipartUploadCallbackVarTest()
        {
            // Initiate a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // Sets the part size as 1M
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // Calculates the part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            // creates a list of PartETag
            var partETags = new List <PartETag>();

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    // skip to the start of each part
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

                    // calculates the part size
                    var size = partSize < partFile.Length - skipBytes
                        ? partSize
                        : partFile.Length - skipBytes;

                    // uploads the part
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

                    // adds the result to the list
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            string callbackHeaderBuilder         = new CallbackHeaderBuilder(_callbackUrl, _callbackBody).Build();
            string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder().
                                                   AddCallbackVariable("x:var1", "x:value1").AddCallbackVariable("x:var2", "x:value2").Build();
            var metadata = new ObjectMetadata();

            metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
            metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder);

            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, _bigObjectKey, initResult.UploadId);

            completeRequest.Metadata = metadata;
            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }
            var completeMultipartUploadResult = _ossClient.CompleteMultipartUpload(completeRequest);

            Assert.IsTrue(completeMultipartUploadResult.IsSetResponseStream());
            Assert.AreEqual(_callbackOkResponse, GetCallbackResponse(completeMultipartUploadResult));
            Assert.AreEqual(completeMultipartUploadResult.HttpStatusCode, HttpStatusCode.OK);
            Assert.AreEqual(completeMultipartUploadResult.ContentLength, _callbackOkResponse.Length);
            Assert.AreEqual(completeMultipartUploadResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ContentType], "application/json");
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ETag], completeMultipartUploadResult.ETag);
            Assert.IsFalse(completeMultipartUploadResult.ResponseMetadata.ContainsKey(HttpHeaders.ContentMd5));
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma].Length > 0);
            Assert.IsTrue(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
            Assert.AreEqual(completeMultipartUploadResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

            //delete the object
            _ossClient.DeleteObject(_bucketName, _bigObjectKey);
        }
예제 #27
0
 private CreateSymlinkCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
                              string bucketName, string symlink, string target, ObjectMetadata metadata)
     : this(client, endpoint, context, bucketName, symlink, target)
 {
     _objectMetadata = metadata;
 }
예제 #28
0
        public static void PutObjectWithHeader(string bucketName)
        {
            const string key = "PutObjectWithHeader";
            try
            {
                using (var content = File.Open(fileToUpload, FileMode.Open))
                {
                    var metadata = new ObjectMetadata();                    
                    metadata.ContentLength = content.Length;

                    metadata.UserMetadata.Add("github-account", "qiyuewuyi");

                    client.PutObject(bucketName, key, content, metadata);

                    Console.WriteLine("Put object:{0} succeeded", key);
                }
            }
            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);
            }
        }
예제 #29
0
        public void MultipartUploadCallbackUriInvalidNegativeTest()
        {
            // initiates a multipart upload
            var initRequest = new InitiateMultipartUploadRequest(_bucketName, _bigObjectKey);
            var initResult  = _ossClient.InitiateMultipartUpload(initRequest);

            // set part size 1MB
            const int partSize = 1024 * 1024 * 1;
            var       partFile = new FileInfo(Config.MultiUploadTestFile);
            // sets part count
            var partCount = OssTestUtils.CalculatePartCount(partFile.Length, partSize);

            var partETags = new List <PartETag>();

            //upload the file
            using (var fs = new FileStream(partFile.FullName, FileMode.Open))
            {
                for (var i = 0; i < partCount; i++)
                {
                    // skip to start position of each part
                    long skipBytes = partSize * i;
                    fs.Position = skipBytes;

                    // sets the part size
                    var size = partSize < partFile.Length - skipBytes
                        ? partSize
                        : partFile.Length - skipBytes;

                    // creates a UploadPartRequest, uploading parts
                    var uploadPartRequest = new UploadPartRequest(_bucketName, _bigObjectKey, initResult.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = (i + 1)
                    };
                    var uploadPartResult = _ossClient.UploadPart(uploadPartRequest);

                    // saves the result to the list
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            var completeRequest = new CompleteMultipartUploadRequest(_bucketName, _bigObjectKey, initResult.UploadId);

            foreach (var partETag in partETags)
            {
                completeRequest.PartETags.Add(partETag);
            }

            string callbackHeaderBuilder = new CallbackHeaderBuilder("https://wwww.aliyun.com/", _callbackBody).Build();
            var    metadata = new ObjectMetadata();

            metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
            completeRequest.Metadata = metadata;

            try
            {
                _ossClient.CompleteMultipartUpload(completeRequest);
                Assert.Fail("Multipart upload callback should be not successfully with invald callback uri");
            }
            catch (OssException e)
            {
                Assert.AreEqual(OssErrorCode.CallbackFailed, e.ErrorCode);
                Assert.AreEqual("Error status : 502.", e.Message);
            }
            finally
            {
                _ossClient.DeleteObject(_bucketName, _bigObjectKey);
            }
        }
예제 #30
0
        public override void ExecuteCommand(CancellationToken token)
        {
            var options = new MockOptionsDictionary();

            var ok = new Action <string, MockOptionsDictionary>((result, checkedOptions) =>
            {
                int numRows = 0;
                if (!int.TryParse(result, out numRows))
                {
                    ShellManager.ShowMessageBox("Please input a valid number");
                    return;
                }
                else
                {
                    if (numRows <= 0)
                    {
                        numRows = 0;
                    }
                    else if (numRows > 1000)
                    {
                        numRows = 1000;
                    }
                }

                string selectedText = ShellManager.GetSelectedText();
                var sb = new StringBuilder();
                using (var ds = new DataSet())
                {
                    QueryManager.Run(ConnectionManager.GetConnectionStringForCurrentWindow(), token, (queryManager) =>
                    {
                        queryManager.Fill(string.Format("SET ROWCOUNT {0}; {1}", numRows, selectedText), ds);
                    });
                    if (ds.Tables.Count == 1)
                    {
                        sb.AppendDropTempTableIfExists("#Actual");
                        sb.AppendLine();
                        sb.AppendDropTempTableIfExists("#Expected");
                        sb.AppendLine();

                        sb.AppendTempTablesFor(ds.Tables[0], "#Actual");
                        sb.Append("INSERT INTO #Actual");

                        ShellManager.AddTextToTopOfSelection(sb.ToString());

                        sb.Clear();
                        sb.AppendColumnNameList(ds.Tables[0]);
                        ShellManager.AppendToEndOfSelection(
                            string.Format("{0}SELECT {1}INTO #Expected{0}FROM #Actual{0}WHERE 1=0;{0}", Environment.NewLine, sb.ToString())
                            );
                        ShellManager.AppendToEndOfSelection(
                            TsqltManager.GenerateInsertFor(ds.Tables[0], ObjectMetadata.FromQualifiedString("#Expected"), false, false));
                    }
                    else
                    {
                        return;
                    }
                }

                //var meta = ObjectMetadata.FromQualifiedString(selectedText);
                //ObjectMetadataAccess da = new ObjectMetadataAccess(ConnectionManager.GetConnectionStringForCurrentWindow());
                //var table = da.SelectTopNFrom(meta, numRows);

                //StringBuilder sb = new StringBuilder();
                //sb.Append(TsqltManager.GetFakeTableStatement(selectedText));
                //sb.AppendLine();
                //sb.Append(TsqltManager.GenerateInsertFor(table, meta, options.EachColumnInSelectOnNewRow, options.EachColumnInValuesOnNewRow));
                //shellManager.ReplaceSelectionWith(sb.ToString());
            });

            var diagManager = new DialogManager.InputWithCheckboxesDialogManager <MockOptionsDictionary>();

            diagManager.Show("How many rows to select? (0=max)", "1", options, ok, cancelCallback);
        }
예제 #31
0
        public void StoreMetadata(ObjectMetadata metadata)
        {
            // validate format of the object name
            if (!ObjectNameValidator.IsValidObjectName(metadata.ObjectName))
            {
                throw new ArgumentException("Invalid object name. It should be 3-30 characters long and contain only alphanumeric characters and underscores.");
            }

            if (null != metadata.Indexes)
            {
                // validate format of the index names
                foreach (var idx in metadata.Indexes)
                {
                    if (!ObjectNameValidator.IsValidIndexName(idx.Name))
                    {
                        throw new ArgumentException("Invalid index name found " + idx.Name + ". It should be 1-30 characters long and contain only alphanumeric characters or underscores.");
                    }
                }
            }

            if (null != metadata.Dependencies)
            {
                // validate # of dependencies
                if (metadata.Dependencies.Length > _config.MaxObjectDependencies)
                {
                    throw new ArgumentException("Maximum number of dependencies exceeded.");
                }

                // validate that dependencies exist
                foreach (var dep in metadata.Dependencies)
                {
                    var fullDepObjName = ObjectNaming.CreateFullObjectName(metadata.NameSpace, dep);
                    if (!Exists(fullDepObjName))
                    {
                        throw new ArgumentException("Object dependency does not exist: " + fullDepObjName);
                    }
                }
            }

            var fullObjName = ObjectNaming.CreateFullObjectName(metadata.NameSpace, metadata.ObjectName);
            var objKey = SerializerHelper.Serialize(fullObjName);
            var metadataVal = SerializerHelper.Serialize<ObjectMetadata>(metadata);
            _store.Set(objKey, metadataVal);
            lock (_mdCache)
            {
                _mdCache[fullObjName] = metadata;
            }

            if (null != ObjectMetadataAdded)
            {
                ObjectMetadataAdded(fullObjName);
            }
        }