Allow to delete bucket
This commit is contained in:
parent
6f7c8439db
commit
b2dce0b8ce
4 changed files with 101 additions and 24 deletions
|
@ -11,6 +11,14 @@ import (
|
||||||
"github.com/minio/minio-go"
|
"github.com/minio/minio-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CopyObjectInfo is the information about an object to copy
|
||||||
|
type CopyObjectInfo struct {
|
||||||
|
BucketName string `json:"bucketName"`
|
||||||
|
ObjectName string `json:"objectName"`
|
||||||
|
SourceBucketName string `json:"sourceBucketName"`
|
||||||
|
SourceObjectName string `json:"sourceObjectName"`
|
||||||
|
}
|
||||||
|
|
||||||
// createBucketHandler creates a new bucket
|
// createBucketHandler creates a new bucket
|
||||||
func createBucketHandler(w http.ResponseWriter, r *http.Request) {
|
func createBucketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var bucket minio.BucketInfo
|
var bucket minio.BucketInfo
|
||||||
|
@ -41,6 +49,18 @@ func createBucketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deleteBucketHandler deletes a bucket
|
||||||
|
func deleteBucketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
|
||||||
|
err := minioClient.RemoveBucket(vars["bucketName"])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
// getObjectHandler downloads an object to the client
|
// getObjectHandler downloads an object to the client
|
||||||
func getObjectHandler(w http.ResponseWriter, r *http.Request) {
|
func getObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -66,6 +86,38 @@ func getObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func createObjectHandler(w http.ResponseWriter, r *http.Request) {
|
func createObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
|
||||||
|
if r.Header.Get("Content-Type") == "application/json" {
|
||||||
|
var copy CopyObjectInfo
|
||||||
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err = r.Body.Close(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, ©)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var copyConds = minio.NewCopyConditions()
|
||||||
|
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
|
||||||
|
fmt.Println(copy)
|
||||||
|
fmt.Println(objectSource)
|
||||||
|
err = minioClient.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
err = json.NewEncoder(w).Encode(copy)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
err := r.ParseMultipartForm(32 << 20)
|
err := r.ParseMultipartForm(32 << 20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -85,6 +137,7 @@ func createObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// deleteObjectHandler deletes an object
|
// deleteObjectHandler deletes an object
|
||||||
func deleteObjectHandler(w http.ResponseWriter, r *http.Request) {
|
func deleteObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -38,6 +38,12 @@ var routes = Routes{
|
||||||
"/api/buckets",
|
"/api/buckets",
|
||||||
createBucketHandler,
|
createBucketHandler,
|
||||||
},
|
},
|
||||||
|
Route{
|
||||||
|
"Delete Bucket",
|
||||||
|
"DELETE",
|
||||||
|
"/api/buckets/{bucketName}",
|
||||||
|
deleteBucketHandler,
|
||||||
|
},
|
||||||
Route{
|
Route{
|
||||||
"Download Object",
|
"Download Object",
|
||||||
"GET",
|
"GET",
|
||||||
|
|
|
@ -2,11 +2,16 @@
|
||||||
<nav class="purple" role="navigation">
|
<nav class="purple" role="navigation">
|
||||||
<div class="nav-wrapper container">
|
<div class="nav-wrapper container">
|
||||||
<span href="#" class="brand-logo"><i class="material-icons">folder_open</i>{{ .BucketName }}</span>
|
<span href="#" class="brand-logo"><i class="material-icons">folder_open</i>{{ .BucketName }}</span>
|
||||||
|
{{ if not .Objects }}
|
||||||
|
<a href="#" class="right" onclick="deleteBucket({{ .BucketName }})"><i class="material-icons">delete</i> Delete</a>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div class="section">
|
<div class="section">
|
||||||
|
<a href="/buckets" style="padding-left: 25px; vertical-align: middle;"><i class="material-icons" style="vertical-align: middle;">arrow_back</i> Buckets</a>
|
||||||
|
|
||||||
|
{{ if .Objects }}
|
||||||
<table class="highlight bordered">
|
<table class="highlight bordered">
|
||||||
|
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -23,7 +28,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
{{ range $index, $object := .Objects }}
|
{{ range $index, $object := .Objects }}
|
||||||
<tr>
|
<tr>
|
||||||
<td><i class="material-icons">{{ $object.Icon }}</i></td>
|
<td style="padding-left: 25px;"><i class="material-icons">{{ $object.Icon }}</i></td>
|
||||||
<td>{{ $object.Key }}</td>
|
<td>{{ $object.Key }}</td>
|
||||||
<td>{{ $object.Size }} bytes</td>
|
<td>{{ $object.Size }} bytes</td>
|
||||||
<td>{{ $object.Owner }}</td>
|
<td>{{ $object.Owner }}</td>
|
||||||
|
@ -37,7 +42,7 @@
|
||||||
<!-- Dropdown Structure -->
|
<!-- Dropdown Structure -->
|
||||||
<ul id="actions-dropdown-{{ $index }}" class="dropdown-content">
|
<ul id="actions-dropdown-{{ $index }}" class="dropdown-content">
|
||||||
<li><a href="/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li>
|
<li><a href="/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li>
|
||||||
<li><a href="#" onclick="deleteObject({{ $object.Key }})">Delete</a></li>
|
<li><a href="#" onclick="deleteObject({{ $.BucketName }}, {{ $object.Key }})">Delete</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -45,11 +50,11 @@
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
{{ if not .Objects }}
|
{{ if not .Objects }}
|
||||||
<p style="text-align: center;margin-top: 2em;">Oh noes... No objects in <strong>{{ .BucketName }}</strong> yet...</p>
|
<p style="text-align: center;margin-top: 2em;">No objects in <strong>{{ .BucketName }}</strong> yet</p>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<p></p>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -86,12 +91,19 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function deleteObject(objectName) {
|
function deleteObject(bucketName, objectName) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'DELETE',
|
type: 'DELETE',
|
||||||
url: '/api/buckets/{{ .BucketName }}/objects/' + objectName,
|
url: '/api/buckets/' + bucketName + '/objects/' + objectName,
|
||||||
success: function () { location.reload(); }
|
success: function () { location.reload(); }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
function deleteBucket(bucketName) {
|
||||||
|
$.ajax({
|
||||||
|
type: 'DELETE',
|
||||||
|
url: '/api/buckets/' + bucketName,
|
||||||
|
success: function () { window.location.replace('/buckets'); }
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
<div class="section">
|
<div class="section">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
||||||
|
{{ if . }}
|
||||||
{{ range $bucket := . }}
|
{{ range $bucket := . }}
|
||||||
<div class="col m12 l6">
|
<div class="col m12 l6">
|
||||||
<a href="/buckets/{{ $bucket.Name }}" style="color: black;">
|
<a href="/buckets/{{ $bucket.Name }}" style="color: black;">
|
||||||
|
@ -30,6 +31,11 @@
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ if not . }}
|
||||||
|
<p style="text-align: center;margin-top: 2em;">No buckets yet</p>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -47,7 +53,7 @@
|
||||||
<br>
|
<br>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col s6">
|
<div class="col s6">
|
||||||
<form id="create-bucket-form" onsubmit="createBucket()">
|
<form id="create-bucket-form">
|
||||||
<div class="input-field">
|
<div class="input-field">
|
||||||
<input placeholder="My Bucket" id="name" type="text" name="name">
|
<input placeholder="My Bucket" id="name" type="text" name="name">
|
||||||
<label for="name">Name</label>
|
<label for="name">Name</label>
|
||||||
|
@ -57,7 +63,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="submit" form="create-bucket-form" class="modal-action modal-close waves-effect waves-green btn-flat">Create</button>
|
<button type="button" onclick="createBucket()" class="modal-action modal-close waves-effect waves-green btn-flat">Create</button>
|
||||||
<button class="modal-action modal-close waves-effect waves-green btn-flat">Cancel</button>
|
<button class="modal-action modal-close waves-effect waves-green btn-flat">Cancel</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue