Skip to content

Commit 7064b99

Browse files
Added option to delete stored file from Amazon S3.
1 parent 5657caa commit 7064b99

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A plugin for multi purpose file upload functionality for Grails 3 application. T
44
to the following destinations:
55

66
1. To the local server or the local system
7-
2. To the Amazon S3 storage (coming soon)
7+
2. To the Amazon S3 storage (can be deleted as well)
88
3. To the Rackspace cloud storage (coming soon)
99

1010
## Installation

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010
}
1111

12-
version "0.0.5"
12+
version "0.0.6"
1313
group "com.wizpanda.plugins"
1414

1515
apply plugin:"eclipse"

grails-app/domain/com/wizpanda/file/StoredFile.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.wizpanda.file
22

3+
import grails.util.Holders
4+
35
class StoredFile {
46

57
String originalName
@@ -9,4 +11,15 @@ class StoredFile {
911
Long size
1012
Date uploadedOn = new Date()
1113
Map meta = [:]
14+
15+
void delete() {
16+
FileUploadService fileUploadService = Holders.getApplicationContext()['fileUploadService']
17+
18+
if (!fileUploadService) {
19+
log.warn "FileUploadService bean not injected!"
20+
return
21+
}
22+
23+
fileUploadService.delete(this)
24+
}
1225
}

grails-app/services/com/wizpanda/file/FileUploadService.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class FileUploadService {
2323
return services.get(groupName).instance().save(file)
2424
}
2525

26+
void delete(StoredFile file) {
27+
if (!file) {
28+
log.warn 'StoredFile is null.'
29+
return
30+
}
31+
32+
services.get(file.groupName).instance().delete(file)
33+
}
34+
2635
@PostConstruct
2736
void verifyConfig() {
2837
//log.debug "Verifying all service"

src/main/groovy/com/wizpanda/file/api/AmazonS3Api.groovy

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.wizpanda.file.StoredFile
44
import com.wizpanda.file.service.AmazonS3UploaderService
55
import grails.util.Environment
66
import grails.util.GrailsStringUtils
7+
import groovy.util.logging.Slf4j
78
import org.jclouds.ContextBuilder
89
import org.jclouds.aws.s3.AWSS3Client
910
import org.jclouds.blobstore.BlobStore
@@ -12,6 +13,7 @@ import org.jclouds.s3.domain.internal.MutableObjectMetadataImpl
1213

1314
import javax.activation.MimetypesFileTypeMap
1415

16+
@Slf4j
1517
abstract class AmazonS3Api extends AbstractStorageApi {
1618

1719
BlobStore blobStore
@@ -20,15 +22,15 @@ abstract class AmazonS3Api extends AbstractStorageApi {
2022
AmazonS3UploaderService service
2123

2224
void authenticate() {
23-
println service.accessKey
24-
println service.accessSecret
2525
context = ContextBuilder.newBuilder("aws-s3")
2626
.credentials(service.accessKey, service.accessSecret)
2727
.buildView(BlobStoreContext.class)
28-
println "Context created ${context.class}"
28+
29+
log.info "Context created ${context.class}"
2930

3031
blobStore = context.getBlobStore()
31-
println "BlobStore ${blobStore.class}"
32+
33+
log.info "BlobStore ${blobStore.class}"
3234

3335
// Storing wrapped Api of S3Client with Apache JCloud
3436
client = context.unwrap().getApi()
@@ -75,6 +77,21 @@ abstract class AmazonS3Api extends AbstractStorageApi {
7577

7678
@Override
7779
void deleteNativeFile(StoredFile file) {
78-
// TODO implement me
80+
String container = this.containerName
81+
String fileName = file.name
82+
83+
log.info "Deleting file ${file} with name ${fileName} from container ${container}."
84+
85+
this.authenticate()
86+
87+
if (!client.objectExists(container, fileName)) {
88+
log.warn "File not present in the S3 bucket."
89+
return
90+
}
91+
92+
client.deleteObject(container, fileName)
93+
file.delete(flush: true)
94+
95+
this.close()
7996
}
8097
}

0 commit comments

Comments
 (0)