Java tutorial
/* * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors: * Michael Vachette */ package org.nuxeo.liveconnect.importer.aws; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; import org.nuxeo.ecm.core.blob.ManagedBlob; import org.nuxeo.ecm.core.blob.SimpleManagedBlob; import org.nuxeo.ecm.liveconnect.core.AbstractLiveConnectBlobProvider; import org.nuxeo.ecm.liveconnect.core.LiveConnectFile; import org.nuxeo.ecm.liveconnect.core.LiveConnectFileInfo; import org.nuxeo.ecm.platform.oauth2.providers.NuxeoOAuth2ServiceProvider; import java.io.IOException; import java.io.InputStream; import java.util.Map; public class LiveconnectS3Blobprovider extends AbstractLiveConnectBlobProvider<NuxeoOAuth2ServiceProvider> { private static final String ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP = "onedrive_document_to_be_updated"; public static final String BUCKET_NAME_PROPERTY = "bucket"; public static final String BUCKET_REGION_PROPERTY = "region"; public static final String AWS_ID_PROPERTY = "awsid"; public static final String AWS_SECRET_PROPERTY = "awssecret"; String bucketName; AmazonS3 amazonS3; public AmazonS3 getClient() { return amazonS3; } public String getBucketName() { return bucketName; } @Override public void initialize(String blobProviderId, Map<String, String> properties) throws IOException { super.initialize(blobProviderId, properties); bucketName = properties.get(BUCKET_NAME_PROPERTY); String bucketRegion = properties.get(BUCKET_REGION_PROPERTY); String awsID = properties.get(AWS_ID_PROPERTY); String awsSecret = properties.get(AWS_SECRET_PROPERTY); amazonS3 = new AmazonS3Client(new BasicAWSCredentials(awsID, awsSecret), new ClientConfiguration()); amazonS3.setRegion(Region.getRegion(Regions.fromName(bucketRegion))); } @Override protected String getCacheName() { return "s3live"; } @Override public InputStream getStream(ManagedBlob blob) throws IOException { LiveConnectFileInfo fileInfo = toFileInfo(blob); GetObjectRequest request = new GetObjectRequest(bucketName, fileInfo.getFileId()); S3Object object = amazonS3.getObject(request); return object.getObjectContent(); } @Override protected String getPageProviderNameForUpdate() { return ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP; } @Override public SimpleManagedBlob toBlob(LiveConnectFileInfo fileInfo) throws IOException { LiveConnectFile file = retrieveFile(fileInfo); return toBlob(file); } @Override protected LiveConnectFile retrieveFile(LiveConnectFileInfo fileInfo) throws IOException { // First, invalidate the Drive file cache in order to force call to API invalidateInCache("file_" + fileInfo.getFileId()); GetObjectRequest request = new GetObjectRequest(bucketName, fileInfo.getFileId()); S3Object object = amazonS3.getObject(request); return new S3LiveConnectFile(fileInfo, object); } }