mirror of
https://github.com/simtactics/mysimulation.git
synced 2025-03-21 17:11:30 +00:00
- NioTSO client isn't needed because we're using RayLib - Added FreeSO's API server to handle most backend operations
54 lines
1.8 KiB
C#
Executable file
54 lines
1.8 KiB
C#
Executable file
using Amazon;
|
|
using Amazon.Runtime;
|
|
using Amazon.S3;
|
|
using Amazon.S3.Model;
|
|
using FSO.Server.Common.Config;
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FSO.Server.Api.Core.Services
|
|
{
|
|
public class AWSUpdateUploader : IUpdateUploader
|
|
{
|
|
private AWSConfig Config;
|
|
public AWSUpdateUploader(AWSConfig config)
|
|
{
|
|
Config = config;
|
|
}
|
|
|
|
public async Task<string> UploadFile(string destPath, string fileName, string groupName)
|
|
{
|
|
var region = Config.Region;
|
|
var bucket = Config.Bucket;
|
|
var s3config = new AmazonS3Config()
|
|
{
|
|
RegionEndpoint = RegionEndpoint.GetBySystemName(region),
|
|
Timeout = new TimeSpan(1, 0, 0),
|
|
ReadWriteTimeout = new TimeSpan(1, 0, 0),
|
|
MaxErrorRetry = 512
|
|
};
|
|
|
|
using (var aws = new AmazonS3Client(new BasicAWSCredentials(Config.AccessKeyID, Config.SecretAccessKey), s3config))
|
|
{
|
|
PutObjectRequest request = new PutObjectRequest()
|
|
{
|
|
InputStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
|
|
BucketName = bucket,
|
|
CannedACL = S3CannedACL.PublicRead,
|
|
Key = destPath
|
|
};
|
|
PutObjectResponse response = await aws.PutObjectAsync(request);
|
|
|
|
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
return $"https://s3.{region}.amazonaws.com/{bucket}/" + destPath;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Uploading file " + destPath + " failed with code " + response.HttpStatusCode + "!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|