S3UploadSettings.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. public sealed class S3UploadCredentials
  6. {
  7. public string AccessKey;
  8. public string SecretKey;
  9. public string SessionToken;
  10. }
  11. [Serializable]
  12. public sealed class S3UploadConfiguration
  13. {
  14. public string Bucket = "ios-games";
  15. public string Region = "us-east-1";
  16. public string AccessKey = "";
  17. public string SecretKey = "";
  18. public string SessionToken = "";
  19. }
  20. public static class S3UploadSettings
  21. {
  22. const string MissingConfigurationMessage = "请在 AppRuntimes 包的 Editor/Config/S3UploadSettings.json 中配置 S3 上传参数。";
  23. public static string ConfigurationPath => AppHelpPackagePaths.FullPath("Editor/Config/S3UploadSettings.json");
  24. public static string Bucket => LoadConfiguration().Bucket;
  25. public static string Region => LoadConfiguration().Region;
  26. public static S3UploadConfiguration LoadConfiguration()
  27. {
  28. if (!File.Exists(ConfigurationPath)) throw new InvalidOperationException(MissingConfigurationMessage);
  29. try
  30. {
  31. return JsonUtility.FromJson<S3UploadConfiguration>(File.ReadAllText(ConfigurationPath))
  32. ?? throw new InvalidOperationException(MissingConfigurationMessage);
  33. }
  34. catch (Exception)
  35. {
  36. // Do not include JSON contents in diagnostics: this file contains credentials.
  37. throw new InvalidOperationException("无法读取包内 S3 配置,请检查 Editor/Config/S3UploadSettings.json。");
  38. }
  39. }
  40. public static S3UploadCredentials LoadCredentials()
  41. {
  42. var configuration = LoadConfiguration();
  43. return new S3UploadCredentials
  44. {
  45. AccessKey = configuration.AccessKey?.Trim(),
  46. SecretKey = configuration.SecretKey?.Trim(),
  47. SessionToken = configuration.SessionToken?.Trim()
  48. };
  49. }
  50. public static void ValidateCredentials(S3UploadCredentials credentials)
  51. {
  52. if (credentials == null || string.IsNullOrWhiteSpace(credentials.AccessKey) || string.IsNullOrWhiteSpace(credentials.SecretKey))
  53. throw new InvalidOperationException(MissingConfigurationMessage);
  54. foreach (var value in new[] { credentials.AccessKey, credentials.SecretKey, credentials.SessionToken })
  55. if (value != null && (value.Contains("\n") || value.Contains("\r"))) throw new ArgumentException("AWS credentials must not contain line breaks.");
  56. }
  57. public static void ValidateDestination(string bucket, string region)
  58. {
  59. if (string.IsNullOrWhiteSpace(bucket) || string.IsNullOrWhiteSpace(region))
  60. throw new ArgumentException(MissingConfigurationMessage);
  61. if (!Regex.IsMatch(bucket, @"\A[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]\z") || bucket.Contains(".."))
  62. throw new ArgumentException("S3 Bucket 格式不正确。");
  63. if (!Regex.IsMatch(region, @"\A[a-z]{2}(?:-[a-z]+)+-\d+\z"))
  64. throw new ArgumentException("S3 Region 格式不正确。");
  65. }
  66. }