Skip to content

Commit

Permalink
add download, get presigned url functionalities for s3 objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrar-Ahmed7 committed Feb 8, 2023
1 parent a6d4e0c commit b30a7fd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 20 deletions.
41 changes: 41 additions & 0 deletions internal/aws/s3.go
Expand Up @@ -3,12 +3,15 @@ package aws
import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/rs/zerolog/log"
)

type BucketResp struct {
Expand Down Expand Up @@ -59,6 +62,44 @@ func GetInfoAboutBucket(sess session.Session, bucketName string, delimiter strin
return result
}

func GetPreSignedUrl(sess session.Session, bucketName, key string) string {
s3Serv := *s3.New(&sess)
req, _ := s3Serv.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
})

url, _ := req.Presign(15 * time.Minute)
return url
}

func DownloadObject(sess session.Session, bucketName, key string) string {
downloader := s3manager.NewDownloader(&sess)
err := os.MkdirAll("./resource/s3/objects", os.ModePerm)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating CSV directory: %v", err))
}
files := strings.Split(key, "/")
objectName := files[len(files)-1]
p := fmt.Sprintf("./resource/s3/objects/%v", objectName)
f, err := os.Create(p)
if err != nil {
fmt.Println("Failed to create file", err)
return ""
}
defer f.Close()
n, err := downloader.Download(f, &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
})
if err != nil {
fmt.Println("failed to download file, err: ", err)
return ""
}

return fmt.Sprintf("%v with size %d bytes, downloaded successfully", objectName, n)
}

func PutObjects(sess session.Session) {
body := strings.NewReader("Hello, I'm working on aws cli!")
s3Serv := *s3.New(&sess)
Expand Down
61 changes: 58 additions & 3 deletions internal/view/buck_obj.go
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"

"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/aws"
"github.com/one2nc/cloud-lens/internal/ui"
"github.com/rs/zerolog/log"
)
Expand All @@ -29,26 +32,78 @@ func (obj *BObj) bindKeys(aa ui.KeyActions) {
ui.KeyShiftC: ui.NewKeyAction("Sort Storage-Class", obj.GetTable().SortColCmd("Storage-Class", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", obj.App().PrevCmd, true),
tcell.KeyEnter: ui.NewKeyAction("View", obj.enterCmd, true),
tcell.KeyCtrlD: ui.NewKeyAction("Download Object", obj.downloadCmd, true),
tcell.KeyCtrlP: ui.NewKeyAction("Pre-Signed URL", obj.preSignedUrlCmd, true),
})
}

func (obj *BObj) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
oName := obj.GetTable().GetSelectedItem()
objName := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()
if fileType == "Folder" {
o := NewS3FileViewer("OBJ")
ctx := obj.App().GetContext()
bn := ctx.Value(internal.BucketName)
fn := fmt.Sprintf("%v%v/", ctx.Value(internal.FolderName), oName)
fn := fmt.Sprintf("%v%v/", ctx.Value(internal.FolderName), objName)
log.Info().Msg(fmt.Sprintf("In view Folder Name: %v", fn))
ctx = context.WithValue(obj.App().context, internal.BucketName, bn)
obj.App().SetContext(ctx)
ctx = context.WithValue(obj.App().context, internal.FolderName, fn)
obj.App().SetContext(ctx)

obj.App().Flash().Info("Bucket Name: " + oName)
obj.App().Flash().Info(fmt.Sprintf("Bucket Name: %v", bn))
// println(bName)
obj.App().inject(o)
}

return nil
}

func (obj *BObj) downloadCmd(evt *tcell.EventKey) *tcell.EventKey {
objName := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()

if fileType == "File" {
ctx := obj.App().GetContext()
op := getObjectParams(ctx, objName)
res := aws.DownloadObject(*op.sess, op.bucketName, op.key)
obj.App().Flash().Info(res)
}

return nil
}

func (obj *BObj) preSignedUrlCmd(evt *tcell.EventKey) *tcell.EventKey {
objNmae := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()

if fileType == "File" {
ctx := obj.App().GetContext()
op := getObjectParams(ctx, objNmae)
url := aws.GetPreSignedUrl(*op.sess, op.bucketName, op.key)
log.Info().Msg(fmt.Sprintf("In view Presigned URL: %v", url))
clipboard.WriteAll(url)
obj.App().Flash().Info("Presigned URL Copied to Clipboard.")
}

return nil
}

func getObjectParams(ctx context.Context, objName string) ObjectParams {
sess, ok := ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
bn := fmt.Sprintf("%v", ctx.Value(internal.BucketName))
fn := fmt.Sprintf("%v", ctx.Value(internal.FolderName))
log.Info().Msg(fmt.Sprintf("In view Bucket Name: %v", bn))
log.Info().Msg(fmt.Sprintf("In view Folder Name: %v", fn))
log.Info().Msg(fmt.Sprintf("In view Object Name: %v", objName))
key := fn + objName
log.Info().Msg(fmt.Sprintf("In view key: %v", key))
return ObjectParams{
sess: sess,
bucketName: bn,
key: key,
}
}
33 changes: 16 additions & 17 deletions internal/view/s3.go
Expand Up @@ -2,12 +2,10 @@ package view

import (
"context"
"fmt"

"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/ui"
"github.com/rs/zerolog/log"
)

type S3 struct {
Expand All @@ -27,34 +25,35 @@ func (s3 *S3) bindKeys(aa ui.KeyActions) {
ui.KeyShiftT: ui.NewKeyAction("Sort Creation-Time", s3.GetTable().SortColCmd("Creation-Time", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", s3.App().PrevCmd, true),
tcell.KeyEnter: ui.NewKeyAction("View", s3.enterCmd, true),
ui.KeyD: ui.NewKeyAction("Describe", s3.describeInstace, true),
ui.KeyD: ui.NewKeyAction("Describe", s3.describeBucket, true),
})
}

func (s3 *S3) describeInstace(evt *tcell.EventKey) *tcell.EventKey {
func (s3 *S3) describeBucket(evt *tcell.EventKey) *tcell.EventKey {
bName := s3.GetTable().GetSelectedItem()
s3.App().Flash().Info("Instance-Id: " + bName)

f := describeResource
if s3.GetTable().enterFn != nil {
f = s3.GetTable().enterFn
}
f(s3.App(), s3.GetTable().GetModel(), s3.Resource(), bName)
if bName != "" {
s3.App().Flash().Info("Bucket-Name: " + bName)
f(s3.App(), s3.GetTable().GetModel(), s3.Resource(), bName)
}

return nil
}

func (s3 *S3) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
bName := s3.GetTable().GetSelectedItem()
o := NewS3FileViewer("OBJ")
log.Info().Msg(fmt.Sprintf("Before Assigning Bucket Name: %v", bName))

ctx := context.WithValue(s3.App().GetContext(), internal.BucketName, bName)
s3.App().SetContext(ctx)
ctx = context.WithValue(s3.App().GetContext(), internal.FolderName, "")
s3.App().SetContext(ctx)
if bName != "" {
o := NewS3FileViewer("OBJ")
ctx := context.WithValue(s3.App().GetContext(), internal.BucketName, bName)
s3.App().SetContext(ctx)
ctx = context.WithValue(s3.App().GetContext(), internal.FolderName, "")
s3.App().SetContext(ctx)
s3.App().Flash().Info("Bucket Name: " + bName)
s3.App().inject(o)
}

s3.App().Flash().Info("After Bucket Name: " + bName)
// println(bName)
s3.App().inject(o)
return nil
}
6 changes: 6 additions & 0 deletions internal/view/types.go
Expand Up @@ -3,6 +3,7 @@ package view
import (
"context"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/one2nc/cloud-lens/internal/model"
"github.com/one2nc/cloud-lens/internal/ui"
)
Expand Down Expand Up @@ -64,3 +65,8 @@ type MetaViewer struct {

// MetaViewers represents a collection of meta viewers.
type MetaViewers map[string]MetaViewer

type ObjectParams struct {
sess *session.Session
bucketName, key string
}

0 comments on commit b30a7fd

Please sign in to comment.