Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fullPath">something like: "C:\Users\mingo\Downloads\Compressed\project_folder\project_folder\model\grid\room.pts"</param>
        /// <param name="relativePath">"model\grid\room.pts"</param>
        public static async Task <bool> UploadArtifaceAsync(ProjectDto project, string fullPath, string relativePath)
        {
            var filePath         = fullPath;
            var fileRelativePath = "project_folder" + relativePath.Replace('\\', '/');

            var api  = new ArtifactsApi();
            var artf = await api.CreateArtifactAsync(project.Owner.Name, project.Name, new KeyRequest(fileRelativePath));

            var url = artf.Url;


            //Use RestSharp
            RestClient  restClient  = new RestClient(url);
            RestRequest restRequest = new RestRequest();

            restRequest.RequestFormat = DataFormat.Json;
            restRequest.Method        = Method.POST;
            restRequest.AddHeader("Content-Type", "multipart/form-data");
            restRequest.AddParameter("AWSAccessKeyId", artf.Fields["AWSAccessKeyId"]);
            restRequest.AddParameter("policy", artf.Fields["policy"]);
            restRequest.AddParameter("signature", artf.Fields["signature"]);
            restRequest.AddParameter("key", artf.Fields["key"]);
            restRequest.AddFile("file", filePath);
            var response = restClient.Execute(restRequest);

            if (response.StatusCode == HttpStatusCode.NoContent)
            {
                Console.WriteLine($"Done uploading: {relativePath}");
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fullPath">something like: "C:\Users\mingo\Downloads\Compressed\project_folder\project_folder\model\grid\room.pts"</param>
        /// <param name="relativePath">"model\grid\room.pts"</param>
        public static async Task <bool> UploadArtifaceAsync(ArtifactsApi api, Project project, string fullPath, string relativePath)
        {
            var filePath         = fullPath;
            var fileRelativePath = relativePath.Replace('\\', '/');

            if (fileRelativePath.StartsWith("/"))
            {
                fileRelativePath = fileRelativePath.Substring(1);
            }

            var artf = await api.CreateArtifactAsync(project.Owner.Name, project.Name, new KeyRequest(fileRelativePath));

            //Use RestSharp
            RestClient restClient = new RestClient
            {
                BaseUrl = new Uri(artf.Url),
                Timeout = Configuration.Default.Timeout
            };

            RestRequest restRequest = new RestRequest
            {
                RequestFormat = DataFormat.Json,
                Method        = Method.POST
            };

            restRequest.AddHeader("Content-Type", "multipart/form-data");

            artf.Fields.Keys.ToList().ForEach(f => restRequest.AddParameter(f, artf.Fields[f]));

            restRequest.AddFile("file", filePath);

            Helper.Logger.Information($"Started upload of {relativePath}");
            var response = await restClient.ExecuteAsync(restRequest);

            if (response.StatusCode == HttpStatusCode.NoContent)
            {
                Helper.Logger.Information($"UploadArtifaceAsync: Done uploading {fileRelativePath}");
                return(true);
            }
            else
            {
                Helper.Logger.Information($"UploadArtifaceAsync: Received response code: {response.StatusCode}");
                Helper.Logger.Information($"{response.Content}");
            }
            return(false);
        }