Created
November 3, 2017 13:56
-
-
Save kknd22/184bfe80b082dfd61e33dd5da601b0cc to your computer and use it in GitHub Desktop.
scala code crud for aws s3 - fakes3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package junk | |
import com.amazonaws.{AmazonClientException, AmazonServiceException, ClientConfiguration} | |
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials} | |
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration | |
import com.amazonaws.services.s3.{AmazonS3Client, AmazonS3ClientBuilder, S3ClientOptions} | |
import com.amazonaws.services.s3.model.Bucket | |
import scala.collection.JavaConversions._ | |
import com.amazonaws.services.s3.model.PutObjectRequest | |
import java.io.{BufferedReader, File, FileOutputStream, IOException, InputStream, InputStreamReader, OutputStreamWriter, PrintWriter, Writer} | |
import com.amazonaws.services.s3.model.GetObjectRequest | |
import com.amazonaws.services.s3.model.S3Object | |
object MyAwsCrud extends App { | |
val bucketName = "my-first-s3-bucket" | |
val key = "MyObjectKey" | |
val credentials: AWSStaticCredentialsProvider = new AWSStaticCredentialsProvider( | |
new BasicAWSCredentials( | |
"foo", | |
"bar")) | |
val s3ClientBuilder = AmazonS3ClientBuilder.standard().withCredentials(credentials) | |
.enablePathStyleAccess().disableChunkedEncoding | |
s3ClientBuilder | |
.setEndpointConfiguration(new EndpointConfiguration("http://localhost:4567", "us-east-1")) | |
val s3Clinet = s3ClientBuilder.build() | |
println("Creating bucket " + bucketName + "\n") | |
s3Clinet.createBucket(bucketName) | |
println("Listing buckets") | |
for (bucket <- s3Clinet.listBuckets) { | |
println(" - " + bucket.getName) | |
} | |
s3Clinet.putObject(new PutObjectRequest(bucketName, key, createSampleFile)) | |
val s3Obj : S3Object = s3Clinet.getObject(new GetObjectRequest(bucketName, key)) | |
println("Content-Type: " + s3Obj.getObjectMetadata.getContentType) | |
displayTextInputStream(s3Obj.getObjectContent) | |
/** | |
* | |
* @throws | |
* @return | |
*/ | |
@throws[IOException]l | |
private def createSampleFile = { | |
val file = File.createTempFile("aws-java-sdk-", ".txt") | |
//file.deleteOnExit() | |
val writer = new PrintWriter(file) | |
writer.write("abcdefghijklmnopqrstuvwxyz\n") | |
writer.write("01234567890112345678901234\n") | |
writer.write("!@#$%^&*()-=[]{};':',.<>/?\n") | |
writer.write("01234567890112345678901234\n") | |
writer.write("abcdefghijklmnopqrstuvwxyz\n") | |
writer.close() | |
file | |
} | |
/** | |
* | |
* @param input | |
* @throws | |
*/ | |
@throws[IOException] | |
private def displayTextInputStream(input: InputStream) { | |
println(scala.io.Source.fromInputStream(input).mkString) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment