From e38079700422193b7c8c002390065ae82fdc3a3d Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 23 Jul 2024 10:46:21 +0500 Subject: [PATCH] s3: allow setting separate bucket name for internal s3 service --- Notesnook.API/Services/S3Service.cs | 29 ++++++++++++++++++----------- Streetwriters.Common/Constants.cs | 4 +++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Notesnook.API/Services/S3Service.cs b/Notesnook.API/Services/S3Service.cs index 1f4f08b..4b50b8c 100644 --- a/Notesnook.API/Services/S3Service.cs +++ b/Notesnook.API/Services/S3Service.cs @@ -42,7 +42,8 @@ namespace Notesnook.API.Services public class S3Service : IS3Service { - private readonly string BUCKET_NAME = "nn-attachments"; + private readonly string BUCKET_NAME = Constants.S3_BUCKET_NAME ?? ""; + private readonly string INTERNAL_BUCKET_NAME = Constants.S3_INTERNAL_BUCKET_NAME ?? ""; private AmazonS3Client S3Client { get; } // When running in a dockerized environment the sync server doesn't have access @@ -96,7 +97,7 @@ namespace Notesnook.API.Services var objectName = GetFullObjectName(userId, name); if (objectName == null) throw new Exception("Invalid object name."); ; - var response = await GetS3Client(S3ClientMode.INTERNAL).DeleteObjectAsync(BUCKET_NAME, objectName); + var response = await GetS3Client(S3ClientMode.INTERNAL).DeleteObjectAsync(GetBucketName(S3ClientMode.INTERNAL), objectName); if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Could not delete object."); @@ -106,7 +107,7 @@ namespace Notesnook.API.Services { var request = new ListObjectsV2Request { - BucketName = BUCKET_NAME, + BucketName = GetBucketName(S3ClientMode.INTERNAL), Prefix = userId, }; @@ -126,10 +127,10 @@ namespace Notesnook.API.Services if (keys.Count <= 0) return; - var deleteObjectsResponse = await S3Client + var deleteObjectsResponse = await GetS3Client(S3ClientMode.INTERNAL) .DeleteObjectsAsync(new DeleteObjectsRequest { - BucketName = BUCKET_NAME, + BucketName = GetBucketName(S3ClientMode.INTERNAL), Objects = keys, }); @@ -169,7 +170,7 @@ namespace Notesnook.API.Services if (string.IsNullOrEmpty(uploadId)) { - var response = await GetS3Client(S3ClientMode.INTERNAL).InitiateMultipartUploadAsync(BUCKET_NAME, objectName); + var response = await GetS3Client(S3ClientMode.INTERNAL).InitiateMultipartUploadAsync(GetBucketName(S3ClientMode.INTERNAL), objectName); if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to initiate multipart upload."); uploadId = response.UploadId; @@ -193,7 +194,7 @@ namespace Notesnook.API.Services var objectName = GetFullObjectName(userId, name); if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload."); - var response = await GetS3Client(S3ClientMode.INTERNAL).AbortMultipartUploadAsync(BUCKET_NAME, objectName, uploadId); + var response = await GetS3Client(S3ClientMode.INTERNAL).AbortMultipartUploadAsync(GetBucketName(S3ClientMode.INTERNAL), objectName, uploadId); if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to abort multipart upload."); } @@ -203,7 +204,7 @@ namespace Notesnook.API.Services if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload."); uploadRequest.Key = objectName; - uploadRequest.BucketName = BUCKET_NAME; + uploadRequest.BucketName = GetBucketName(S3ClientMode.INTERNAL); var response = await GetS3Client(S3ClientMode.INTERNAL).CompleteMultipartUploadAsync(uploadRequest); if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to complete multipart upload."); } @@ -215,7 +216,7 @@ namespace Notesnook.API.Services var request = new GetPreSignedUrlRequest { - BucketName = BUCKET_NAME, + BucketName = GetBucketName(mode), Expires = System.DateTime.Now.AddHours(1), Verb = httpVerb, Key = objectName, @@ -231,9 +232,9 @@ namespace Notesnook.API.Services private string GetPresignedURLForUploadPart(string objectName, string uploadId, int partNumber) { - return GetS3Client().GetPreSignedURL(new GetPreSignedUrlRequest + return GetS3Client(S3ClientMode.INTERNAL).GetPreSignedURL(new GetPreSignedUrlRequest { - BucketName = BUCKET_NAME, + BucketName = GetBucketName(S3ClientMode.INTERNAL), Expires = System.DateTime.Now.AddHours(1), Verb = HttpVerb.PUT, Key = objectName, @@ -263,5 +264,11 @@ namespace Notesnook.API.Services if (mode == S3ClientMode.INTERNAL && S3InternalClient != null) return S3InternalClient; return S3Client; } + + string GetBucketName(S3ClientMode mode = S3ClientMode.EXTERNAL) + { + if (mode == S3ClientMode.INTERNAL && S3InternalClient != null) return INTERNAL_BUCKET_NAME; + return BUCKET_NAME; + } } } \ No newline at end of file diff --git a/Streetwriters.Common/Constants.cs b/Streetwriters.Common/Constants.cs index 2425b73..4a557c1 100644 --- a/Streetwriters.Common/Constants.cs +++ b/Streetwriters.Common/Constants.cs @@ -30,6 +30,9 @@ namespace Streetwriters.Common public static string S3_ACCESS_KEY_ID => Environment.GetEnvironmentVariable("S3_ACCESS_KEY_ID"); public static string S3_SERVICE_URL => Environment.GetEnvironmentVariable("S3_SERVICE_URL"); public static string S3_REGION => Environment.GetEnvironmentVariable("S3_REGION"); + public static string S3_BUCKET_NAME => Environment.GetEnvironmentVariable("S3_BUCKET_NAME"); + public static string S3_INTERNAL_BUCKET_NAME => Environment.GetEnvironmentVariable("S3_INTERNAL_BUCKET_NAME"); + public static string S3_INTERNAL_SERVICE_URL => Environment.GetEnvironmentVariable("S3_INTERNAL_SERVICE_URL"); // SMTP settings public static string SMTP_USERNAME => Environment.GetEnvironmentVariable("SMTP_USERNAME"); @@ -70,7 +73,6 @@ namespace Streetwriters.Common // internal public static string MONGODB_CONNECTION_STRING => Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING"); public static string MONGODB_DATABASE_NAME => Environment.GetEnvironmentVariable("MONGODB_DATABASE_NAME"); - public static string S3_INTERNAL_SERVICE_URL => Environment.GetEnvironmentVariable("S3_INTERNAL_SERVICE_URL"); public static int SUBSCRIPTIONS_SERVER_PORT => int.Parse(Environment.GetEnvironmentVariable("SUBSCRIPTIONS_SERVER_PORT")); public static string SUBSCRIPTIONS_SERVER_HOST => Environment.GetEnvironmentVariable("SUBSCRIPTIONS_SERVER_HOST"); public static string SUBSCRIPTIONS_SERVER_DOMAIN => Environment.GetEnvironmentVariable("SUBSCRIPTIONS_SERVER_DOMAIN");