@@ -38,17 +38,12 @@ public CloudflareR2Service(IConfiguration configuration, ILogger<CloudflareR2Ser
3838 endpoint , _bucketName
3939 ) ;
4040
41- _logger . LogInformation (
42- "Access Key ID: {Preview} | Length: {Length}" ,
43- accessKeyId . Length > 8 ? accessKeyId [ ..8 ] + "..." : "***" , accessKeyId . Length
44- ) ;
41+ // Don't log access key details in production
4542
4643 // Extract Account ID from endpoint (for diagnostics only)
4744 var match = Regex . Match ( endpoint , @"https://([a-f0-9]+)\.r2\.cloudflarestorage\.com" ) ;
4845 _accountId = match . Success ? match . Groups [ 1 ] . Value : "unknown" ;
4946
50- _logger . LogInformation ( "Detected R2 Account ID: {AccountId}" , _accountId ) ;
51-
5247 // Initialize MinIO client for Cloudflare R2
5348 var endpointUri = new Uri ( endpoint ) ;
5449 _minioClient = new MinioClient ( )
@@ -67,17 +62,13 @@ public async Task<string> UploadFileAsync(
6762 {
6863 try
6964 {
70- _logger . LogInformation ( "Starting upload to R2: {ObjectKey}, Size: {Size} bytes, ContentType: {ContentType}" ,
71- objectKey , file . Length , file . ContentType ) ;
72-
7365 // Ensure bucket exists
7466 var bucketExists = await _minioClient . BucketExistsAsync (
7567 new BucketExistsArgs ( ) . WithBucket ( _bucketName ) ,
7668 cancellationToken ) ;
7769
7870 if ( ! bucketExists )
7971 {
80- _logger . LogWarning ( "Bucket {BucketName} does not exist, creating it..." , _bucketName ) ;
8172 await _minioClient . MakeBucketAsync (
8273 new MakeBucketArgs ( ) . WithBucket ( _bucketName ) ,
8374 cancellationToken ) ;
@@ -94,12 +85,10 @@ await _minioClient.PutObjectAsync(
9485 . WithContentType ( file . ContentType ) ,
9586 cancellationToken ) ;
9687
97- _logger . LogInformation ( "Successfully uploaded object {Key} to R2" , objectKey ) ;
9888 return objectKey ;
9989 }
10090 catch ( Exception ex )
10191 {
102- _logger . LogError ( ex , "Error uploading file to R2: {ObjectKey}" , objectKey ) ;
10392 throw ;
10493 }
10594 }
@@ -115,12 +104,10 @@ await _minioClient.RemoveObjectAsync(
115104 . WithBucket ( _bucketName )
116105 . WithObject ( objectKey ) ,
117106 cancellationToken ) ;
118- _logger . LogInformation ( "Successfully deleted object {Key} from R2" , objectKey ) ;
119107 return true ;
120108 }
121109 catch ( Exception ex )
122110 {
123- _logger . LogError ( ex , "Error deleting file from R2: {ObjectKey}" , objectKey ) ;
124111 return false ;
125112 }
126113 }
@@ -141,7 +128,6 @@ public async Task<string> GetPresignedUrlAsync(
141128 }
142129 catch ( Exception ex )
143130 {
144- _logger . LogError ( ex , "Error generating presigned URL for R2: {ObjectKey}" , objectKey ) ;
145131 throw ;
146132 }
147133 }
@@ -167,7 +153,6 @@ await _minioClient.GetObjectAsync(
167153 }
168154 catch ( Exception ex )
169155 {
170- _logger . LogError ( ex , "Error getting file stream from R2: {ObjectKey}" , objectKey ) ;
171156 throw ;
172157 }
173158 }
@@ -176,51 +161,26 @@ public async Task<bool> TestConnectionAsync(CancellationToken cancellationToken
176161 {
177162 try
178163 {
179- _logger . LogInformation (
180- "Testing R2 connection by checking bucket {Bucket}" ,
181- _bucketName
182- ) ;
183-
184164 var exists = await _minioClient . BucketExistsAsync (
185165 new BucketExistsArgs ( ) . WithBucket ( _bucketName ) ,
186166 cancellationToken ) ;
187167
188168 if ( exists )
189169 {
190- _logger . LogInformation ( "R2 connection test successful - bucket {Bucket} exists" , _bucketName ) ;
170+ _logger . LogInformation ( "R2 connection test successful - bucket exists" ) ;
191171 }
192172 else
193173 {
194- _logger . LogWarning ( "Bucket {Bucket} does not exist, but connection is working" , _bucketName ) ;
174+ _logger . LogWarning ( "Bucket does not exist, but connection is working" ) ;
195175 }
196176
197177 return true ;
198178 }
199179 catch ( Exception ex )
200180 {
201181 _logger . LogError (
202- ex ,
203- "R2 connection test failed | Message: {Message}" ,
204- ex . Message
182+ "R2 connection test failed"
205183 ) ;
206-
207- if ( ex . Message . Contains ( "InvalidAccessKeyId" ) || ex . Message . Contains ( "Access Denied" ) || ex . Message . Contains ( "403" ) )
208- {
209- _logger . LogError ( "═══════════════════════════════════════════════════════════" ) ;
210- _logger . LogError ( "R2 AUTHENTICATION ERROR" ) ;
211- _logger . LogError ( "═══════════════════════════════════════════════════════════" ) ;
212- _logger . LogError ( "The Access Key ID does not exist in Cloudflare R2 records." ) ;
213- _logger . LogError ( "" ) ;
214- _logger . LogError ( "TROUBLESHOOTING STEPS:" ) ;
215- _logger . LogError ( "1. Verify your endpoint Account ID: {AccountId}" , _accountId ) ;
216- _logger . LogError ( "2. Go to Cloudflare Dashboard → R2 → Manage R2 API Tokens" ) ;
217- _logger . LogError ( "3. Check which Account ID your API token was created for" ) ;
218- _logger . LogError ( "4. If different, create a NEW API token for Account ID: {AccountId}" , _accountId ) ;
219- _logger . LogError ( "5. Update appsettings.Development.json with the new credentials" ) ;
220- _logger . LogError ( "6. Ensure the token has 'Object Read & Write' permissions" ) ;
221- _logger . LogError ( "═══════════════════════════════════════════════════════════" ) ;
222- }
223-
224184 return false ;
225185 }
226186 }
0 commit comments