@@ -51,7 +51,7 @@ export class S3Service {
5151 : undefined ;
5252 }
5353
54- async initialize ( ) {
54+ async initialize ( policy : 'public' | 'private' ) {
5555 const [ , err ] = await catchError ( async ( ) => {
5656 addLog . info ( `Checking bucket: ${ this . config . bucket } ` ) ;
5757 const bucketExists = await this . client . bucketExists ( this . config . bucket ) ;
@@ -60,46 +60,70 @@ export class S3Service {
6060 addLog . info ( `Creating bucket: ${ this . config . bucket } ` ) ;
6161 const [ , err ] = await catchError ( ( ) => this . client . makeBucket ( this . config . bucket ) ) ;
6262 if ( err ) {
63- addLog . warn ( `Failed to create bucket: ${ this . config . bucket } ` ) ;
64- return Promise . reject ( err ) ;
63+ addLog . error ( `Failed to create bucket: ${ this . config . bucket } ` ) ;
64+ return ;
6565 }
6666 }
6767
68- if ( this . config . retentionDays && this . config . retentionDays > 0 ) {
69- const Days = this . config . retentionDays ;
70- const [ , err ] = await catchError ( ( ) =>
71- Promise . all ( [
72- this . client . setBucketPolicy (
73- this . config . bucket ,
74- JSON . stringify ( {
75- Version : '2012-10-17' ,
76- Statement : [
77- {
78- Effect : 'Allow' ,
79- Principal : '*' ,
80- Action : [ 's3:GetObject' ] ,
81- Resource : [ `arn:aws:s3:::${ this . config . bucket } /*` ]
82- }
83- ]
84- } )
85- ) ,
86- this . client . setBucketLifecycle ( this . config . bucket , {
87- Rule : [
68+ const [ _ , err ] = await catchError ( async ( ) => {
69+ if ( policy === 'public' ) {
70+ return this . client . setBucketPolicy (
71+ this . config . bucket ,
72+ JSON . stringify ( {
73+ Version : '2012-10-17' ,
74+ Statement : [
8875 {
89- ID : 'AutoDeleteRule' ,
90- Status : 'Enabled' ,
91- Expiration : {
92- Days,
93- DeleteMarker : false ,
94- DeleteAll : false
95- }
76+ Effect : 'Allow' ,
77+ Principal : '*' ,
78+ Action : [ 's3:GetObject' ] ,
79+ Resource : [ `arn:aws:s3:::${ this . config . bucket } /*` ]
9680 }
9781 ]
9882 } )
99- ] )
83+ ) ;
84+ }
85+ if ( policy === 'private' ) {
86+ return this . client . setBucketPolicy (
87+ this . config . bucket ,
88+ JSON . stringify ( {
89+ Version : '2012-10-17' ,
90+ Statement : [ ]
91+ } )
92+ ) ;
93+ }
94+ } ) ;
95+ if ( err ) {
96+ addLog . warn ( `Failed to set bucket policy: ${ this . config . bucket } ` ) ;
97+ }
98+
99+ // Update bucket lifecycle
100+ if ( this . config . retentionDays && this . config . retentionDays > 0 ) {
101+ const Days = this . config . retentionDays ;
102+ const [ , err ] = await catchError ( ( ) =>
103+ this . client . setBucketLifecycle ( this . config . bucket , {
104+ Rule : [
105+ {
106+ ID : 'AutoDeleteRule' ,
107+ Status : 'Enabled' ,
108+ Expiration : {
109+ Days,
110+ DeleteMarker : false ,
111+ DeleteAll : false
112+ }
113+ }
114+ ]
115+ } )
116+ ) ;
117+ if ( err ) {
118+ addLog . warn ( `Failed to set bucket lifecycle: ${ this . config . bucket } ` ) ;
119+ }
120+ } else {
121+ // Remove bucket policy to make it private
122+ const [ , err ] = await catchError ( ( ) =>
123+ this . client . removeBucketLifecycle ( this . config . bucket )
100124 ) ;
101125 if ( err ) {
102- addLog . warn ( `Failed to set bucket policy : ${ this . config . bucket } ` ) ;
126+ addLog . warn ( `Failed to remove bucket lifecycle : ${ this . config . bucket } ` ) ;
103127 }
104128 }
105129
0 commit comments