FM/Assets/Scripts/FUJIFILM/Other/OSSResourceUploader.cs

71 lines
2.6 KiB
C#
Raw Normal View History

2025-09-13 17:03:16 +08:00
using UnityEngine;
using System;
using System.IO;
using System.Threading.Tasks;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace HK.FUJIFILM
{
public static class OSSResourceUploaderHelper
{
public static readonly string accessKeyId = "LTAI5t9AcXJm2BXwzLTie8Wa";
public static readonly string accessKeySecret = "RJhANJfNL7Xr8EmEZfMR12ED5nIp8c";
public static readonly string endpoint = "oss-cn-hongkong.aliyuncs.com";
public static readonly string bucketName = "kiosk-assets-bundle";
public static readonly string ossRootDirectory = "FUJIFILM/"; // OSS上的资源根存储目录
public static readonly string ossRelativePath = "GameRes/v1.0/"; // OSS上的资源相对存储目录
}
public class OSSResourceUploader : ManagerBase<OSSResourceUploader>
{
// 阿里云OSS配置
private OssClient _ossClient;
public OSSResourceUploader()
{
try
{
// 初始化OSS客户端
_ossClient = new OssClient(OSSResourceUploaderHelper.endpoint, OSSResourceUploaderHelper.accessKeyId,
OSSResourceUploaderHelper.accessKeySecret);
Debug.Log("OSS客户端初始化成功");
}
catch (Exception ex)
{
Debug.LogError($"OSS客户端初始化失败: {ex.Message}");
}
}
// 递归上传目录
public void UploadDirectory(string localDirectory,
string ossRelativePath, // string ossRelativePath = "GameRes/v1.0/";
Action<string, string> onComplete,
Action<long, long> onProgress = null)
{
if (_ossClient == null)
{
onComplete?.Invoke(null, "OSS客户端未初始化");
return;
}
// 上传目录下的所有文件
foreach (var filePath in Directory.GetFiles(localDirectory))
{
string fileName = Path.GetFileName(filePath);
string ossObjectKey = $"{OSSResourceUploaderHelper.ossRootDirectory}{ossRelativePath}{fileName}";
// 上传文件
_ossClient.PutObject(OSSResourceUploaderHelper.bucketName, ossObjectKey, filePath);
Debug.Log($"已上传: {ossObjectKey}");
}
// 递归上传子目录
foreach (var subDirectory in Directory.GetDirectories(localDirectory))
{
string dirName = Path.GetFileName(subDirectory) + "/";
UploadDirectory(subDirectory, ossRelativePath + dirName, onComplete, onProgress);
}
}
}
}