Skip to content

Commit

Permalink
Merge pull request #1253 from bcgov/dev-rook-RQ-FOIMOD-3551
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
richard-aot authored Jan 6, 2025
2 parents 59a93eb + 35bff50 commit 7047c94
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions computingservices/OpenInfoServices/lib/awslib/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
smithyendpoints "github.com/aws/smithy-go/endpoints"
)

Expand Down Expand Up @@ -232,12 +233,24 @@ func CopyS3(sourceBucket string, sourcePrefix string, filemappings []AdditionalF
Bucket: aws.String(destBucket),
CopySource: aws.String(bucket + "/" + sourceKey),
Key: aws.String(destKey),
ACL: types.ObjectCannedACLPublicRead,
})
if err != nil {
log.Fatalf("unable to copy item %q, %v", sourceKey, err)
}

fmt.Printf("Copied %s to %s\n", sourceKey, destKey)

// // Ensure the copied object is publicly readable
// _, err = svc.PutObjectAcl(context.TODO(), &s3.PutObjectAclInput{
// Bucket: aws.String(destBucket),
// Key: aws.String(destKey),
// ACL: types.ObjectCannedACLPublicRead,
// })
// if err != nil {
// fmt.Println("Error setting object ACL:", err)
// return
// }
}

fmt.Println("All files copied successfully!")
Expand All @@ -264,6 +277,30 @@ func SaveFileS3(openInfoBucket string, openInfoPrefix string, filename string, b
return err
}

func SaveXMLS3(openInfoBucket string, openInfoPrefix string, filename string, buf []byte) error {
bucket := openInfoBucket //"dev-openinfopub"
prefix := openInfoPrefix //"poc/packages/HSG_2024_40515/" // Folder prefix in the bucket

svc := CreateS3Client()

// Upload the HTML content to S3
_, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(prefix + filename),
Body: bytes.NewReader(buf),
ContentType: aws.String("application/xml"),
ContentDisposition: aws.String("inline"),
ACL: types.ObjectCannedACLPublicRead,
})
if err != nil {
fmt.Println("Error uploading file:", err)
} else {
fmt.Println("File uploaded successfully!")
}

return err
}

func GetFileFromS3(openInfoBucket string, openInfoPrefix string, filename string) *s3.GetObjectOutput {
bucket := openInfoBucket //"dev-openinfopub"
prefix := openInfoPrefix //"poc/packages/HSG_2024_40515/" // Folder prefix in the bucket
Expand Down Expand Up @@ -324,7 +361,7 @@ func SaveSiteMapIndexS3(openInfoBucket string, openInfoPrefix string, filename s
updatedXML = append(xmlHeader, updatedXML...)
updatedXML = bytes.Replace(updatedXML, []byte("<sitemapindex>"), []byte("<sitemapindex"+string(xmlns)+">"), 1)

return SaveFileS3(openInfoBucket, openInfoPrefix, filename, updatedXML)
return SaveXMLS3(openInfoBucket, openInfoPrefix, filename, updatedXML)
}

func SaveSiteMapPageS3(openInfoBucket string, openInfoPrefix string, filename string, updatedurlset UrlSet) error {
Expand All @@ -340,7 +377,7 @@ func SaveSiteMapPageS3(openInfoBucket string, openInfoPrefix string, filename st
updatedXML = append(xmlHeader, updatedXML...)
updatedXML = bytes.Replace(updatedXML, []byte("<urlset>"), []byte("<urlset"+string(xmlns)+">"), 1)

return SaveFileS3(openInfoBucket, openInfoPrefix, filename, updatedXML)
return SaveXMLS3(openInfoBucket, openInfoPrefix, filename, updatedXML)
}

func RemoveFromS3(openInfoBucket string, openInfoPrefix string) error {
Expand Down

0 comments on commit 7047c94

Please sign in to comment.