Skip to content

Commit

Permalink
moving and renaming files
Browse files Browse the repository at this point in the history
Signed-off-by: Finbarrs Oketunji <f@finbarrs.eu>
  • Loading branch information
0xnu committed Aug 23, 2023
1 parent 89d6331 commit bbc5935
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 8 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Changelog

## 1.0.8 - 2023-08-22
* Remove Unit Tests
* Bug Fixes
## 1.0.9 - 2023-08-23
* Moving and Renaming Files
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION=1.0.8
COMMIT_MSG ?= "README"
VERSION=1.0.9
COMMIT_MSG ?= "moving and renaming files"

execute: ## Execute Locally
@go mod init s3interact
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ make package
- [x] ~~Bucket Policies and Permissions~~
- [x] ~~Bucket Deletion~~
- [x] ~~Set a new Region~~
- [ ] Moving and Renaming Files
- [x] ~~Moving and Renaming Files~~
- [ ] Moving and Renaming Folders

### Contributing
Expand Down
Binary file modified build/darwin_amd64/s3interact-cli_darwin_amd64
Binary file not shown.
Binary file modified build/linux_amd64/s3interact-cli_linux_amd64
Binary file not shown.
Binary file modified build/windows_amd64/s3interact-cli_windows_amd64.exe
Binary file not shown.
51 changes: 51 additions & 0 deletions s3_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,54 @@ func setRegion(svc *s3.S3, region string) {
svc.Config.Region = aws.String(region)
fmt.Println("Region set successfully to:", region)
}

func moveFiles(svc *s3.S3, bucket, sourceFolder, destinationFolder string, fileKeys []string) {
for _, fileKey := range fileKeys {
sourceKey := sourceFolder + "/" + fileKey
destinationKey := destinationFolder + "/" + fileKey

_, err := svc.CopyObject(&s3.CopyObjectInput{
Bucket: aws.String(bucket),
CopySource: aws.String(bucket + "/" + sourceKey),
Key: aws.String(destinationKey),
})
if err != nil {
fmt.Println("Error copying file:", err)
continue
}

_, err = svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(sourceKey),
})
if err != nil {
fmt.Println("Error deleting original file:", err)
continue
}

fmt.Printf("File %s moved successfully from %s to %s.\n", fileKey, sourceFolder, destinationFolder)
}
}

func renameFile(svc *s3.S3, bucket, originalKey, newKey string) {
_, err := svc.CopyObject(&s3.CopyObjectInput{
Bucket: aws.String(bucket),
CopySource: aws.String(bucket + "/" + originalKey),
Key: aws.String(newKey),
})
if err != nil {
fmt.Println("Error copying file:", err)
return
}

_, err = svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(originalKey),
})
if err != nil {
fmt.Println("Error deleting original file:", err)
return
}

fmt.Printf("File %s renamed successfully to %s.\n", originalKey, newKey)
}
40 changes: 38 additions & 2 deletions s3interact.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func main() {
"14": setBucketACLAction,
"15": deleteBucketAction,
"16": setRegionAction,
"17": moveFilesAction,
"18": renameFileAction,
}

for {
Expand All @@ -88,15 +90,17 @@ func main() {
fmt.Println("14. Set Bucket ACL")
fmt.Println("15. Delete Bucket")
fmt.Println("16. Set a Region")
fmt.Println("17. Exit")
fmt.Println("17. Move a File")
fmt.Println("18. Rename a File")
fmt.Println("19. Exit")
fmt.Print("Enter your choice: ")
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(choice)

action, exists := actions[choice]
if exists {
action(svc, bucket, reader)
} else if choice == "17" {
} else if choice == "19" {
return
} else {
fmt.Println("Invalid choice. Please try again.")
Expand Down Expand Up @@ -216,3 +220,35 @@ func setRegionAction(svc *s3.S3, bucket string, reader *bufio.Reader) {
newRegion = strings.TrimSpace(newRegion)
setRegion(svc, newRegion)
}

func moveFilesAction(svc *s3.S3, bucket string, reader *bufio.Reader) {
fmt.Print("Enter source folder: ")
sourceFolder, _ := reader.ReadString('\n')
sourceFolder = strings.TrimSpace(sourceFolder)

fmt.Print("Enter destination folder: ")
destinationFolder, _ := reader.ReadString('\n')
destinationFolder = strings.TrimSpace(destinationFolder)

fmt.Print("Enter file keys to move (comma-separated): ")
fileKeysInput, _ := reader.ReadString('\n')
fileKeys := strings.Split(strings.TrimSpace(fileKeysInput), ",")

for i, key := range fileKeys {
fileKeys[i] = strings.TrimSpace(key)
}

moveFiles(svc, bucket, sourceFolder, destinationFolder, fileKeys)
}

func renameFileAction(svc *s3.S3, bucket string, reader *bufio.Reader) {
fmt.Print("Enter original file key: ")
originalKey, _ := reader.ReadString('\n')
originalKey = strings.TrimSpace(originalKey)

fmt.Print("Enter new file key: ")
newKey, _ := reader.ReadString('\n')
newKey = strings.TrimSpace(newKey)

renameFile(svc, bucket, originalKey, newKey)
}

0 comments on commit bbc5935

Please sign in to comment.