github.com/minio/minio-go/v6@v6.0.57/examples/s3/put-encrypted-object.go (about)

     1  // +build ignore
     2  
     3  /*
     4   * MinIO Go Library for Amazon S3 Compatible Cloud Storage
     5   * Copyright 2015-2017 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"log"
    24  	"os"
    25  
    26  	"github.com/minio/minio-go/v6"
    27  	"github.com/minio/minio-go/v6/pkg/encrypt"
    28  )
    29  
    30  func main() {
    31  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-testfile, my-bucketname and
    32  	// my-objectname are dummy values, please replace them with original values.
    33  
    34  	// Requests are always secure (HTTPS) by default. Set secure=false to enable insecure (HTTP) access.
    35  	// This boolean value is the last argument for New().
    36  
    37  	// New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically
    38  	// determined based on the Endpoint value.
    39  	s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    40  	if err != nil {
    41  		log.Fatalln(err)
    42  	}
    43  
    44  	filePath := "my-testfile" // Specify a local file that we will upload
    45  
    46  	// Open a local file that we will upload
    47  	file, err := os.Open(filePath)
    48  	if err != nil {
    49  		log.Fatalln(err)
    50  	}
    51  	defer file.Close()
    52  
    53  	// Get file stats.
    54  	fstat, err := file.Stat()
    55  	if err != nil {
    56  		log.Fatalln(err)
    57  	}
    58  
    59  	bucketname := "my-bucketname"              // Specify a bucket name - the bucket must already exist
    60  	objectName := "my-objectname"              // Specify a object name
    61  	password := "correct horse battery staple" // Specify your password. DO NOT USE THIS ONE - USE YOUR OWN.
    62  
    63  	// New SSE-C where the cryptographic key is derived from a password and the objectname + bucketname as salt
    64  	encryption := encrypt.DefaultPBKDF([]byte(password), []byte(bucketname+objectName))
    65  
    66  	// Encrypt file content and upload to the server
    67  	n, err := s3Client.PutObject(bucketname, objectName, file, fstat.Size(), minio.PutObjectOptions{ServerSideEncryption: encryption})
    68  	if err != nil {
    69  		log.Fatalln(err)
    70  	}
    71  
    72  	log.Println("Uploaded", "my-objectname", " of size: ", n, "Successfully.")
    73  }