unreal 读取文件,并上传到aws s3

void UUploadBPLibrary::Upload(FString FilePath)
{
	FString FileName = FPaths::GetCleanFilename(FilePath);

	TArray FileData;
	if (!FFileHelper::LoadFileToArray(FileData, *FilePath))
	{
		UE_LOG(LogTemp, Error, TEXT("Failed to load file"));
		return;
	}
	UE_LOG(LogTemp, Warning, TEXT("file data %d"), FileData.Num());
	FString EncodedData = FBase64::Encode(FileData);

	// Set up the request headers and URL
	TSharedRef HttpRequest = FHttpModule::Get().CreateRequest();

	// Create the JSON object to hold the file data
	TSharedPtr JsonObject = MakeShareable(new FJsonObject);

	FString RequestBody;
	// Create a binary writer to stream the file contents
	TSharedRef> JsonWriter = TJsonWriterFactory<>::Create(&RequestBody);
	JsonWriter->WriteObjectStart();
	JsonWriter->WriteValue(TEXT("buffer"), EncodedData);
	JsonWriter->WriteValue("fileName", FileName);
	JsonWriter->WriteObjectEnd();
	JsonWriter->Close();

	const FString url = "https://xxxxx.us-east-1.amazonaws.com/S3/bucket/upload";
	HttpRequest->SetHeader("Content-Type", TEXT("application/json"));
	HttpRequest->SetURL(url);
	HttpRequest->SetVerb("POST");
	UE_LOG(LogTemp, Warning, TEXT(" request body %s"), *RequestBody);
	HttpRequest->SetContentAsString(RequestBody);

	HttpRequest->OnRequestProgress().BindLambda([=](FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived)
	{
		float Progress = static_cast(BytesReceived) / static_cast(HttpRequest->GetResponse()->GetContentLength());
		UE_LOG(LogTemp, Warning, TEXT("File downloaded  %f"), Progress);
	});
	HttpRequest->OnProcessRequestComplete().BindLambda([=](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess)
	{
		UE_LOG(LogTemp, Warning, TEXT(" response %s"), *Response.Get()->GetContentAsString());
	});
	HttpRequest->ProcessRequest();
}

aws lambda function,用nodejs写的, 处理接收的buffer

const AWS = require('aws-sdk')


var s3 = new AWS.S3();

exports.handler = async function (event, context) {
    // 
    const buffer= Buffer.from(event.buffer, 'base64');
    let fileName = event.fileName;
    let r = await Upload(fileName, buffer);


    return r;
};
var Upload = function (fileName, buffer) {
    return s3.upload({
        Bucket: 'content', Key: fileName, Body: buffer
    }).promise();
}

你可能感兴趣的:(游戏,unreal,engine,4,unreal,engine,5,unreal)