Ejemplo n.º 1
0
 public StorageApiClient(string token, string runId = "", string userAgent = "sapi-dotnet-client")
 {
     _token  = token;
     _client = new HttpMultipartDataClient(ServerAddress,
                                           new Dictionary <string, string>()
     {
         { HeaderTokenName, _token },
         { HeaderRunIdName, runId },
         { HeaderUserAgentName, userAgent }
     }, "/v2");
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Uploads file to S3 via SAPI
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="isPublic"></param>
        /// <param name="notify"></param>
        /// <returns></returns>
        public string UploadFile(string filePath, bool isPublic = false, bool notify = true)
        {
            // 1. prepare resource
            FileInfo finfo    = new FileInfo(filePath);
            var      postData = new Dictionary <string, string>()
            {
                { "isPublic", isPublic.ToString() },
                { "notify", notify.ToString() },
                { "name", finfo.Name },
                { "sizeBytes", finfo.Length.ToString() }
            };

            _client.AddStringFormData(postData);
            //_client.AddFileFormData("data", file);
            string prepareUri   = "/storage/files/prepare";
            Stream result       = _client.SendPostRequestReadAsStream(prepareUri);
            string strResponse  = new StreamReader(result).ReadToEnd();
            var    json         = Newtonsoft.Json.Linq.JObject.Parse(strResponse);
            var    uploadParams = json["uploadParams"];


            // 2. upload directly do S3 using returned credentials
            var s3Client = new HttpMultipartDataClient(uploadParams["url"].ToObject <string>(), new Dictionary <string, string>());

            postData = new Dictionary <string, string>()
            {
                { "key", uploadParams["key"].ToObject <string>() },
                { "acl", uploadParams["acl"].ToObject <string>() },
                { "signature", uploadParams["signature"].ToObject <string>() },
                { "policy", uploadParams["policy"].ToObject <string>() },
                { "AWSAccessKeyId", uploadParams["AWSAccessKeyId"].ToObject <string>() }
            };
            string uploadUri = "/";

            s3Client.AddStringFormData(postData, false);
            s3Client.AddFileFormData("file", finfo);
            s3Client.SendPostRequestReadAsStream(uploadUri);

            return(json["id"].ToObject <string>());
        }