C# 範例程式碼
以下程式碼使用 curl 轉換器撰寫,請斟酌參考
取得 Presigned URL
using System.Net.Http;
using System.Net.Http.Headers;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.maiagent.ai/api/v1/upload-presigned-url/");
request.Headers.Add("Authorization", "Api-Key ");
request.Content = new StringContent("{\n \"filename\": \"Products_example.xlsx\",\n \"modelName\": \"chatbot-file\",\n \"fieldName\": \"file\",\n \"fileSize\": 5881\n}");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
上傳檔案至雲端儲存
using System.Net.Http;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://s3.ap-northeast-1.amazonaws.com/whizchat-media-prod-django.playma.app");
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StringContent("media/chatbots/chatbot-file/d83c88aa-3874-49a6-b01e-1948ae48b747.xlsx"), "key");
content.Add(new StringContent("AWS4-HMAC-SHA256"), "x-amz-algorithm");
content.Add(new StringContent("AKIATIVCN4X5JXWAP43M/20241129/ap-northeast-1/s3/aws4_request"), "x-amz-credential");
content.Add(new StringContent("20241129T012318Z"), "x-amz-date");
content.Add(new StringContent("eyJleHBpcmF0aW9uIjogIjIwMjQtMTEtMjlUMDI6MjM6MThaIiwgImNvbmRpdGlvbnMiOiBbWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzYwMF0sIHsiYnVja2V0IjogIndoaXpjaGF0LW1lZGlhLXByb2QtZGphbmdvLnBsYXltYS5hcHAifSwgeyJrZXkiOiAibWVkaWEvY2hhdGJvdHMvY2hhdGJvdC1maWxlL2Q4M2M4OGFhLTM4NzQtNDlhNi1iMDFlLTE5NDhhZTQ4Yjc0Ny54bHN4In0sIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwgeyJ4LWFtei1jcmVkZW50aWFsIjogIkFLSUFUSVZDTjRYNUpYV0FQNDNNLzIwMjQxMTI5L2FwLW5vcnRoZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LCB7IngtYW16LWRhdGUiOiAiMjAyNDExMjlUMDEyMzE4WiJ9XX0="), "policy");
content.Add(new StringContent("0254b3baa65c3a69eaeeccd0a3d027bc7952c1587cc96437c096eb1b76e1d692"), "x-amz-signature");
content.Add(new ByteArrayContent(File.ReadAllBytes("/Users/maiagent/Downloads/Products_example.png")), "file", Path.GetFileName("/Users/maiagent/Downloads/Products_example.png"));
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
確認檔案上傳與提交檔案資訊
using System.Net.Http;
using System.Net.Http.Headers;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.maiagent.ai/api/chatbots/484a07fe-b401-4a32-9077-ddaae0c5e862/files/");
request.Headers.Add("Authorization", "Api-Key <api-key>");
request.Content = new StringContent("{\n \"files\": [\n {\n \"file\": \"media/chatbots/chatbot-file/d83c88aa-3874-49a6-b01e-1948ae48b747.xlsx\",\n \"filename\": \"Products_example.xlsx\"\n }\n ]\n}");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
以上三個請求請 AI 整合後為以下 Service
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class FileUploadService
{
private readonly HttpClient _client;
private readonly string _apiKey;
public FileUploadService(string apiKey)
{
_client = new HttpClient();
_apiKey = apiKey;
}
public async Task UploadFileAsync(string filePath, string chatbotId)
{
try
{
// 1. 取得 Presigned URL
var presignedUrlInfo = await GetPresignedUrlAsync(filePath);
// 2. 上傳檔案至雲端儲存
await UploadToCloudStorageAsync(filePath, presignedUrlInfo);
// 3. 確認檔案上傳與提交檔案資訊
await ConfirmFileUploadAsync(presignedUrlInfo, chatbotId);
}
catch (Exception ex)
{
throw new Exception($"檔案上傳過程發生錯誤: {ex.Message}");
}
}
private async Task<PresignedUrlInfo> GetPresignedUrlAsync(string filePath)
{
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.maiagent.ai/api/v1/upload-presigned-url/");
request.Headers.Add("Authorization", $"Api-Key {_apiKey}");
var fileInfo = new FileInfo(filePath);
var content = new
{
filename = fileInfo.Name,
modelName = "chatbot-file",
fieldName = "file",
fileSize = fileInfo.Length
};
request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(content));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();
return await System.Text.Json.JsonSerializer.DeserializeAsync<PresignedUrlInfo>(
await response.Content.ReadAsStreamAsync());
}
private async Task UploadToCloudStorageAsync(string filePath, PresignedUrlInfo presignedInfo)
{
var request = new HttpRequestMessage(HttpMethod.Post, presignedInfo.UploadUrl);
var content = new MultipartFormDataContent();
content.Add(new StringContent(presignedInfo.Key), "key");
content.Add(new StringContent(presignedInfo.Algorithm), "x-amz-algorithm");
content.Add(new StringContent(presignedInfo.Credential), "x-amz-credential");
content.Add(new StringContent(presignedInfo.Date), "x-amz-date");
content.Add(new StringContent(presignedInfo.Policy), "policy");
content.Add(new StringContent(presignedInfo.Signature), "x-amz-signature");
content.Add(new ByteArrayContent(File.ReadAllBytes(filePath)), "file", Path.GetFileName(filePath));
request.Content = content;
var response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();
}
private async Task ConfirmFileUploadAsync(PresignedUrlInfo presignedInfo, string chatbotId)
{
var request = new HttpRequestMessage(HttpMethod.Post,
$"https://api.maiagent.ai/api/chatbots/{chatbotId}/files/");
request.Headers.Add("Authorization", $"Api-Key {_apiKey}");
var content = new
{
files = new[]
{
new
{
file = presignedInfo.Key,
filename = presignedInfo.Filename
}
}
};
request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(content));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();
}
}
// 用於存儲 Presigned URL 相關資訊的類別
public class PresignedUrlInfo
{
public string UploadUrl { get; set; }
public string Key { get; set; }
public string Algorithm { get; set; }
public string Credential { get; set; }
public string Date { get; set; }
public string Policy { get; set; }
public string Signature { get; set; }
public string Filename { get; set; }
}
呼叫使用方法
// 初始化服務
var uploadService = new FileUploadService("your-api-key-here");
// 上傳檔案
await uploadService.UploadFileAsync(
filePath: "/path/to/your/file.xlsx",
chatbotId: "484a07fe-b401-4a32-9077-ddaae0c5e862"
);
Last updated
Was this helpful?