Skip to content

Commit ba75eea

Browse files
Presign every link to a resource in case the bucket is private
1 parent 9b5cb9d commit ba75eea

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

dotnet/src/dotnetframework/GxClasses/Services/Storage/ExternalProviderBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public abstract class ExternalProviderBase
1818
protected static String DEFAULT_ACL = "DEFAULT_ACL";
1919
protected static String DEFAULT_EXPIRATION = "DEFAULT_EXPIRATION";
2020
protected static String FOLDER = "FOLDER_NAME";
21-
protected static String DEFAULT_ACL_DEPRECATED = "STORAGE_PROVIDER_PRIVACY";
21+
protected static String DEFAULT_STORAGE_PRIVACY = "STORAGE_PROVIDER_PRIVACY";
2222
protected static String DEFAULT_EXPIRATION_DEPRECATED = "STORAGE_PROVIDER_DEFAULT_EXPIRATION";
2323
protected TimeSpan defaultExpiration = new TimeSpan(24, 0, 0);
2424
protected static string DEFAULT_TMP_CONTENT_TYPE = "image/jpeg";
@@ -52,7 +52,7 @@ public ExternalProviderBase(GXService s)
5252

5353
private void Initialize()
5454
{
55-
String aclS = GetPropertyValue(DEFAULT_ACL, DEFAULT_ACL_DEPRECATED, "");
55+
String aclS = GetPropertyValue(DEFAULT_ACL, DEFAULT_STORAGE_PRIVACY, "");
5656
if (!String.IsNullOrEmpty(aclS))
5757
{
5858
this.defaultAcl = aclS.Equals("Private") ? GxFileType.Private : GxFileType.PublicRead;

dotnet/src/dotnetframework/Providers/Storage/GXAmazonS3/ExternalProviderS3.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class ExternalProviderS3 : ExternalProviderBase, ExternalProvider
5151
bool customEndpoint = false;
5252

5353
bool objectOwnershipEnabled;
54+
private enum BucketPrivacy { PRIVATE, PUBLIC };
55+
private BucketPrivacy ownerEnforcedBucketPrivacy;
5456

5557
public string StorageUri
5658
{
@@ -106,7 +108,11 @@ private void Initialize() {
106108
}
107109
}
108110

109-
objectOwnershipEnabled = !GetPropertyValue(DEFAULT_ACL, DEFAULT_ACL_DEPRECATED, "").Equals("Bucket owner enforced");
111+
string default_storage_privacy = GetPropertyValue(DEFAULT_ACL, DEFAULT_STORAGE_PRIVACY, "");
112+
objectOwnershipEnabled = !default_storage_privacy.Contains("Bucket owner enforced");
113+
ownerEnforcedBucketPrivacy = (BucketPrivacy) (!objectOwnershipEnabled ?
114+
(default_storage_privacy.Contains("private") ? BucketPrivacy.PRIVATE : BucketPrivacy.PUBLIC)
115+
: (BucketPrivacy?) null);
110116

111117
#if NETCORE
112118
if (credentials != null)
@@ -243,7 +249,10 @@ public string Upload(string localFile, string objectName, GxFileType fileType)
243249

244250
private bool IsPrivateUpload(GxFileType fileType)
245251
{
246-
return (GetCannedACL(fileType) != S3CannedACL.PublicRead) && objectOwnershipEnabled;
252+
if (objectOwnershipEnabled && GetCannedACL(fileType) != S3CannedACL.PublicRead)
253+
return true;
254+
else
255+
return ownerEnforcedBucketPrivacy == BucketPrivacy.PRIVATE;
247256
}
248257

249258
public string Get(string objectName, GxFileType fileType, int urlMinutes = 0)
@@ -264,7 +273,7 @@ public string GetUrl(string objectName, GxFileType fileType, int urlMinutes = 0)
264273
private string GetUrlImpl(string objectName, GxFileType fileType, int urlMinutes = 0)
265274
{
266275
bool isPrivate = IsPrivateUpload(fileType);
267-
return (isPrivate)? GetPreSignedUrl(objectName, ResolveExpiration(urlMinutes).TotalMinutes): StorageUri + StorageUtils.EncodeUrlPath(objectName);
276+
return (isPrivate) ? GetPreSignedUrl(objectName, ResolveExpiration(urlMinutes).TotalMinutes): IsPrivateUpload(fileType) ? GetPreSignedUrl(objectName, ResolveExpiration(urlMinutes).TotalMinutes): StorageUri + StorageUtils.EncodeUrlPath(objectName);
268277

269278
}
270279

@@ -323,7 +332,9 @@ public string Rename(string objectName, string newName, GxFileType fileType)
323332
{
324333
Copy(objectName, fileType, newName, fileType);
325334
Delete(objectName, fileType);
326-
return StorageUri + StorageUtils.EncodeUrlPath(newName);
335+
if (objectOwnershipEnabled)
336+
return StorageUri + StorageUtils.EncodeUrlPath(newName);
337+
return IsPrivateUpload(fileType) ? GetPreSignedUrl(objectName, defaultExpiration.Minutes) : StorageUri + StorageUtils.EncodeUrlPath(newName);
327338
}
328339

329340
public string Copy(string objectName, GxFileType sourceFileType, string newName, GxFileType destFileType)
@@ -348,7 +359,9 @@ public string Copy(string objectName, GxFileType sourceFileType, string newName,
348359
}
349360

350361
CopyObject(request);
351-
return StorageUri + StorageUtils.EncodeUrlPath(newName);
362+
if (objectOwnershipEnabled)
363+
return StorageUri + StorageUtils.EncodeUrlPath(newName);
364+
return IsPrivateUpload(sourceFileType) ? GetPreSignedUrl(objectName, defaultExpiration.Minutes) : StorageUri + StorageUtils.EncodeUrlPath(newName);
352365
}
353366

354367
private S3CannedACL GetCannedACL(GxFileType acl)
@@ -466,7 +479,9 @@ public string Copy(string url, string newName, string tableName, string fieldNam
466479
AddObjectMetadata(request.Metadata, tableName, fieldName, resourceKey);
467480
CopyObject(request);
468481

469-
return StorageUri + StorageUtils.EncodeUrlPath(resourceKey);
482+
if (objectOwnershipEnabled)
483+
return StorageUri + StorageUtils.EncodeUrlPath(resourceKey);
484+
return IsPrivateUpload(destFileType) ? GetPreSignedUrl(resourceKey, defaultExpiration.Minutes) : StorageUri + StorageUtils.EncodeUrlPath(resourceKey);
470485
}
471486

472487
public string Save(Stream fileStream, string fileName, string tableName, string fieldName, GxFileType destFileType)
@@ -493,7 +508,9 @@ public string Save(Stream fileStream, string fileName, string tableName, string
493508
AddObjectMetadata(objectRequest.Metadata, tableName, fieldName, resourceKey);
494509
PutObjectResponse result = PutObject(objectRequest);
495510

496-
return StorageUri + resourceKey;
511+
if (objectOwnershipEnabled)
512+
return StorageUri + resourceKey;
513+
return IsPrivateUpload(destFileType) ? GetPreSignedUrl(resourceKey, defaultExpiration.Minutes) : StorageUri + StorageUtils.EncodeUrlPath(resourceKey);
497514
}
498515
catch (Exception ex)
499516
{

0 commit comments

Comments
 (0)