/// <summary> /// Copy or Replace a file stored in B2. This will copy the file on B2's servers, resulting in no download or upload charges. /// </summary> /// <param name="sourceFileId"></param> /// <param name="newFileName"></param> /// <param name="metadataDirective">COPY or REPLACE. COPY will not allow any changes to File Info or Content Type. REPLACE will.</param> /// <param name="contentType"></param> /// <param name="fileInfo"></param> /// <param name="range">byte range to copy.</param> /// <param name="cancelToken"></param> /// <returns></returns> public async Task <B2File> Copy(string sourceFileId, string newFileName, B2MetadataDirective metadataDirective = B2MetadataDirective.COPY, string contentType = "", Dictionary <string, string> fileInfo = null, string range = "", string destinationBucketId = "", CancellationToken cancelToken = default(CancellationToken)) { if (metadataDirective == B2MetadataDirective.COPY && (!string.IsNullOrWhiteSpace(contentType) || fileInfo != null)) { throw new CopyReplaceSetupException("Copy operations cannot specify fileInfo or contentType."); } if (metadataDirective == B2MetadataDirective.REPLACE && (string.IsNullOrWhiteSpace(contentType) || fileInfo == null)) { throw new CopyReplaceSetupException("Replace operations must specify fileInfo and contentType."); } var request = FileCopyRequestGenerators.Copy(_options, sourceFileId, newFileName, metadataDirective, contentType, fileInfo, range, destinationBucketId); // Send the download request var response = await _client.SendAsync(request, cancelToken); // Create B2File from response return(await ResponseParser.ParseResponse <B2File>(response, _api)); }
public static HttpRequestMessage Copy(B2Options options, string sourceFileId, string fileName, B2MetadataDirective metadataDirective, string contentType = "", Dictionary <string, string> fileInfo = null, string range = "", string destinationBucketId = "") { var uri = new Uri(options.ApiUrl + "/b2api/" + Constants.Version + "/" + Endpoints.Copy); var payload = new Dictionary <string, object>() { { "sourceFileId", sourceFileId }, { "fileName", fileName }, { "metadataDirective", metadataDirective.ToString() }, }; if (!string.IsNullOrWhiteSpace(range)) { payload.Add("range", range); } if (!string.IsNullOrWhiteSpace(destinationBucketId)) { payload.Add("destinationBucketId", destinationBucketId); } if (metadataDirective == B2MetadataDirective.REPLACE) { if (string.IsNullOrWhiteSpace(contentType)) { payload.Add("contentType", "b2/x-auto"); } else { payload.Add("contentType", contentType); } } // File Info if (metadataDirective == B2MetadataDirective.REPLACE && fileInfo != null && fileInfo.Count > 0) { payload.Add("fileInfo", fileInfo); } var json = JsonConvert.SerializeObject(payload); var request = new HttpRequestMessage() { Method = HttpMethod.Post, RequestUri = uri, Content = new StringContent(json) }; request.Headers.TryAddWithoutValidation("Authorization", options.AuthorizationToken); return(request); }