digitalocean spaces example

import boto3

# Initialize a session using Spaces
session = boto3.session.Session()
client = session.client('s3',
                        region_name='ams3',
                        endpoint_url='https://ams3.digitaloceanspaces.com',
                        aws_access_key_id='xxx',
                        aws_secret_access_key='yyy')

unique_name = ['play', 'strim', 'strim-in', 'crowd']
for s in unique_name:
    client.create_bucket(Bucket=s)

# List all Spaces in the region
# response = client.list_buckets()
# for s in [space['Name'] for space in response['Buckets']]:
#     print(s)

# Add a file to a Space
res = client.upload_file('/home/icdu/projects/link/amazonTest/test.docx', 'play', 'test2.docx')
print res
Advertisements

digitalocean spaces java example

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class Test {

    private static final String SUFFIX = "/";

    public static void main(String[] args) {
        AWSCredentials credentials = new BasicAWSCredentials(
                "xxx",
                "yyy");

        AmazonS3 s3client = new AmazonS3Client(credentials);
        s3client.setEndpoint("https://ams3.digitaloceanspaces.com");
        String bucketName = "ubi-example-bucket";
        s3client.createBucket(bucketName);

        for (Bucket bucket : s3client.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }

        String folderName = "testfolder";
        createFolder(bucketName, folderName, s3client);

        String fileName = folderName + SUFFIX + "test.docx";
        s3client.putObject(new PutObjectRequest(bucketName, fileName,
                new File("/home/icdu/projects/link/amazonTest/test.docx"))
                .withCannedAcl(CannedAccessControlList.PublicRead));

//        deleteFolder(bucketName, folderName, s3client);
//        s3client.deleteBucket(bucketName);
    }

    public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(0);

        InputStream emptyContent = new ByteArrayInputStream(new byte[0]);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
                folderName + SUFFIX, emptyContent, metadata);

        client.putObject(putObjectRequest);
    }


//    public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) {
//        List fileList =
//                client.listObjects(bucketName, folderName).getObjectSummaries();
//        for (S3ObjectSummary file : fileList) {
//            client.deleteObject(bucketName, file.getKey());
//        }
//        client.deleteObject(bucketName, folderName);
//    }
}

Tomcat 7 JDBC Session Persistence

as root run:

export CATALINA_OPTS=”-Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true”

create table in mysql:

createtabletomcat_sessions (
  session_id     varchar(100) notnullprimarykey,
  valid_session  char(1) notnull,
  max_inactive   intnotnull,
  last_access    bigintnotnull,
  app_name       varchar(255),
  session_data   mediumblob,
  KEYkapp_name(app_name)
);
cd /usr/share/java
wget http://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar
cd /usr/share/tomcat7/lib and run:
/usr/share/tomcat7/lib# ln -s ../../java/mysql-connector.jar mysql-connector.jar
in META-INF create context.xml
paste Manager section:
<Context antiJARLocking="true" >
    <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
        virtualClasspath="/home/link/config"
        searchVirtualFirst="true" />

    <Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleBackup="10">
        <Store className="org.apache.catalina.session.JDBCStore"
               connectionURL="jdbc:mysql://localhost/tomcat?user=root&amp;password=password"
               driverName="com.mysql.jdbc.Driver"
               sessionAppCol="app_name"
               sessionDataCol="session_data"
               sessionIdCol="session_id"
               sessionLastAccessedCol="last_access"
               sessionMaxInactiveCol="max_inactive"
               sessionTable="tomcat_sessions"
               sessionValidCol="valid_session" />
    </Manager>
</Context>