list s3 bucket

This commit is contained in:
JurajKubrican
2024-11-18 16:18:36 +01:00
parent e9081c9bdc
commit c1ffce5c3a
9 changed files with 150 additions and 36 deletions

View File

@@ -3,9 +3,11 @@ package main
import (
"html/template"
"io"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
type Templates struct {
@@ -29,9 +31,21 @@ func newPage() Page {
return Page{}
}
type Article struct {
Content string
}
func newArticle(content string) Article {
return Article{
Content: content,
}
}
func main() {
e := echo.New()
e.Renderer = NewTemplates()
e.Logger.SetLevel(log.DEBUG)
e.Use(middleware.Logger())
page := newPage()
@@ -43,5 +57,16 @@ func main() {
return c.Render(200, "index", page)
})
e.GET("/we-are-here", func(c echo.Context) error {
// Log a test message
e.Logger.Info("Test log: /we-are-here endpoint called")
msg := getNotes(c, e.Logger)
e.Logger.Info(msg)
article := newArticle(" content")
return c.Render(http.StatusOK, "article", article)
})
e.Logger.Fatal(e.Start(":54321"))
}

35
src/notes.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/labstack/echo/v4"
)
func getNotes(c echo.Context, logger echo.Logger) string {
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(c.Request().Context(), config.WithRegion("eu-west-1"))
if err != nil {
logger.Fatal(err)
}
// Create an Amazon S3 service client with the specified endpoint
client := s3.NewFromConfig(cfg)
// Get the first page of results for ListObjectsV2 for a bucket
output, err := client.ListObjectsV2(c.Request().Context(), &s3.ListObjectsV2Input{
Bucket: aws.String("obsidian-notes-jurajk"),
Prefix: aws.String("pub"),
})
if err != nil {
logger.Fatal(err)
}
logger.Info("first page results:")
for _, object := range output.Contents {
logger.Printf("key=%s size=%d", aws.ToString(object.Key), object.Size)
}
return "success"
}