mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Add missing Apache 2 License
This commit is contained in:
parent
9edab1215b
commit
0de7efabd4
@ -216,6 +216,10 @@ public class OssStorageResource implements WritableResource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a bucket.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public Bucket createBucket() {
|
public Bucket createBucket() {
|
||||||
return this.oss.createBucket(this.bucketName);
|
return this.oss.createBucket(this.bucketName);
|
||||||
}
|
}
|
||||||
@ -226,8 +230,8 @@ public class OssStorageResource implements WritableResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取一个OutputStream用于写操作。
|
* acquire an OutputStream for write.
|
||||||
* 注意:写完成后必须关闭该流
|
* Note: please close the stream after writing is done
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@ -242,17 +246,21 @@ public class OssStorageResource implements WritableResource {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ossObject = this.getOSSObject();
|
ossObject = this.getOSSObject();
|
||||||
} catch (OSSException ex) {
|
}
|
||||||
if (ex.getMessage() != null && ex.getMessage().startsWith(MESSAGE_KEY_NOT_EXIST)) {
|
catch (OSSException ex) {
|
||||||
|
if (ex.getMessage() != null
|
||||||
|
&& ex.getMessage().startsWith(MESSAGE_KEY_NOT_EXIST)) {
|
||||||
ossObject = null;
|
ossObject = null;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ossObject == null ) {
|
if (ossObject == null) {
|
||||||
if (!this.autoCreateFiles) {
|
if (!this.autoCreateFiles) {
|
||||||
throw new FileNotFoundException("The object was not found: " + getURI());
|
throw new FileNotFoundException(
|
||||||
|
"The object was not found: " + getURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -263,7 +271,8 @@ public class OssStorageResource implements WritableResource {
|
|||||||
executorService.submit(() -> {
|
executorService.submit(() -> {
|
||||||
try {
|
try {
|
||||||
OssStorageResource.this.oss.putObject(bucketName, objectKey, in);
|
OssStorageResource.this.oss.putObject(bucketName, objectKey, in);
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
logger.error("Failed to put object", ex);
|
logger.error("Failed to put object", ex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package com.alibaba.alicloud.oss.resource;
|
package com.alibaba.alicloud.oss.resource;
|
||||||
|
|
||||||
import com.aliyun.oss.model.Bucket;
|
import com.aliyun.oss.model.Bucket;
|
||||||
@ -14,7 +30,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lich
|
* @author lich
|
||||||
* @date 2019/8/30
|
|
||||||
*/
|
*/
|
||||||
public class DummyOssClient {
|
public class DummyOssClient {
|
||||||
|
|
||||||
@ -26,22 +41,25 @@ public class DummyOssClient {
|
|||||||
return String.join(".", bucketName, objectKey);
|
return String.join(".", bucketName, objectKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PutObjectResult putObject(String bucketName, String objectKey, InputStream inputStream) {
|
public PutObjectResult putObject(String bucketName, String objectKey,
|
||||||
|
InputStream inputStream) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] result = StreamUtils.copyToByteArray(inputStream);
|
byte[] result = StreamUtils.copyToByteArray(inputStream);
|
||||||
storeMap.put(getStoreKey(bucketName, objectKey), result);
|
storeMap.put(getStoreKey(bucketName, objectKey), result);
|
||||||
} catch (IOException ex) {
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
} finally {
|
}
|
||||||
|
finally {
|
||||||
try {
|
try {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
} catch (IOException ex) {
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new PutObjectResult();
|
return new PutObjectResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package com.alibaba.alicloud.oss.resource;
|
package com.alibaba.alicloud.oss.resource;
|
||||||
|
|
||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.common.utils.IOUtils;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
@ -19,7 +34,6 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@ -28,9 +42,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lich
|
* @author lich
|
||||||
* @date 2019/8/29
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
public class OssStorageResourceTest {
|
public class OssStorageResourceTest {
|
||||||
@ -59,7 +71,7 @@ public class OssStorageResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testResourceType() {
|
public void testResourceType() {
|
||||||
assertEquals(OssStorageResource.class, remoteResource.getClass());
|
assertEquals(OssStorageResource.class, remoteResource.getClass());
|
||||||
OssStorageResource ossStorageResource = (OssStorageResource)remoteResource;
|
OssStorageResource ossStorageResource = (OssStorageResource) remoteResource;
|
||||||
assertEquals("myfilekey", ossStorageResource.getFilename());
|
assertEquals("myfilekey", ossStorageResource.getFilename());
|
||||||
assertFalse(ossStorageResource.isBucket());
|
assertFalse(ossStorageResource.isBucket());
|
||||||
}
|
}
|
||||||
@ -67,39 +79,41 @@ public class OssStorageResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testValidObject() throws Exception {
|
public void testValidObject() throws Exception {
|
||||||
assertTrue(remoteResource.exists());
|
assertTrue(remoteResource.exists());
|
||||||
OssStorageResource ossStorageResource = (OssStorageResource)remoteResource;
|
OssStorageResource ossStorageResource = (OssStorageResource) remoteResource;
|
||||||
assertTrue(ossStorageResource.bucketExists());
|
assertTrue(ossStorageResource.bucketExists());
|
||||||
assertEquals(4096L, remoteResource.contentLength());
|
assertEquals(4096L, remoteResource.contentLength());
|
||||||
assertEquals("oss://aliyun-test-bucket/myfilekey", remoteResource.getURI().toString());
|
assertEquals("oss://aliyun-test-bucket/myfilekey",
|
||||||
|
remoteResource.getURI().toString());
|
||||||
assertEquals("myfilekey", remoteResource.getFilename());
|
assertEquals("myfilekey", remoteResource.getFilename());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBucketResource() throws Exception {
|
public void testBucketResource() throws Exception {
|
||||||
assertTrue(bucketResource.exists());
|
assertTrue(bucketResource.exists());
|
||||||
assertTrue(((OssStorageResource)this.bucketResource).isBucket());
|
assertTrue(((OssStorageResource) this.bucketResource).isBucket());
|
||||||
assertTrue(((OssStorageResource)this.bucketResource).bucketExists());
|
assertTrue(((OssStorageResource) this.bucketResource).bucketExists());
|
||||||
assertEquals("oss://aliyun-test-bucket/", bucketResource.getURI().toString());
|
assertEquals("oss://aliyun-test-bucket/", bucketResource.getURI().toString());
|
||||||
assertEquals("aliyun-test-bucket", this.bucketResource.getFilename());
|
assertEquals("aliyun-test-bucket", this.bucketResource.getFilename());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBucketNotEndingInSlash() {
|
public void testBucketNotEndingInSlash() {
|
||||||
assertTrue(new OssStorageResource(this.oss, "oss://aliyun-test-bucket").isBucket());
|
assertTrue(
|
||||||
|
new OssStorageResource(this.oss, "oss://aliyun-test-bucket").isBucket());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpecifyPathCorrect() {
|
public void testSpecifyPathCorrect() {
|
||||||
OssStorageResource ossStorageResource = new OssStorageResource (
|
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
|
||||||
this.oss, "oss://aliyun-test-bucket/myfilekey", false);
|
"oss://aliyun-test-bucket/myfilekey", false);
|
||||||
|
|
||||||
assertTrue(ossStorageResource.exists());
|
assertTrue(ossStorageResource.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpecifyBucketCorrect() {
|
public void testSpecifyBucketCorrect() {
|
||||||
OssStorageResource ossStorageResource = new OssStorageResource(
|
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
|
||||||
this.oss, "oss://aliyun-test-bucket", false);
|
"oss://aliyun-test-bucket", false);
|
||||||
|
|
||||||
assertTrue(ossStorageResource.isBucket());
|
assertTrue(ossStorageResource.isBucket());
|
||||||
assertEquals("aliyun-test-bucket", ossStorageResource.getBucket().getName());
|
assertEquals("aliyun-test-bucket", ossStorageResource.getBucket().getName());
|
||||||
@ -109,14 +123,16 @@ public class OssStorageResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testBucketOutputStream() throws IOException {
|
public void testBucketOutputStream() throws IOException {
|
||||||
this.expectedEx.expect(IllegalStateException.class);
|
this.expectedEx.expect(IllegalStateException.class);
|
||||||
this.expectedEx.expectMessage("Cannot open an output stream to a bucket: 'oss://aliyun-test-bucket/'");
|
this.expectedEx.expectMessage(
|
||||||
|
"Cannot open an output stream to a bucket: 'oss://aliyun-test-bucket/'");
|
||||||
((WritableResource) this.bucketResource).getOutputStream();
|
((WritableResource) this.bucketResource).getOutputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBucketInputStream() throws IOException {
|
public void testBucketInputStream() throws IOException {
|
||||||
this.expectedEx.expect(IllegalStateException.class);
|
this.expectedEx.expect(IllegalStateException.class);
|
||||||
this.expectedEx.expectMessage("Cannot open an input stream to a bucket: 'oss://aliyun-test-bucket/'");
|
this.expectedEx.expectMessage(
|
||||||
|
"Cannot open an input stream to a bucket: 'oss://aliyun-test-bucket/'");
|
||||||
this.bucketResource.getInputStream();
|
this.bucketResource.getInputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +146,8 @@ public class OssStorageResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testBucketFile() throws IOException {
|
public void testBucketFile() throws IOException {
|
||||||
this.expectedEx.expect(UnsupportedOperationException.class);
|
this.expectedEx.expect(UnsupportedOperationException.class);
|
||||||
this.expectedEx.expectMessage("oss://aliyun-test-bucket/ cannot be resolved to absolute file path");
|
this.expectedEx.expectMessage(
|
||||||
|
"oss://aliyun-test-bucket/ cannot be resolved to absolute file path");
|
||||||
this.bucketResource.getFile();
|
this.bucketResource.getFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +194,16 @@ public class OssStorageResourceTest {
|
|||||||
assertEquals(expectedString, actualString);
|
assertEquals(expectedString, actualString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateBucket() {
|
||||||
|
String location = "oss://my-new-test-bucket/";
|
||||||
|
OssStorageResource resource = new OssStorageResource(this.oss, location, true);
|
||||||
|
|
||||||
|
resource.createBucket();
|
||||||
|
|
||||||
|
assertTrue(resource.bucketExists());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for the tests.
|
* Configuration for the tests.
|
||||||
@ -191,23 +217,16 @@ public class OssStorageResourceTest {
|
|||||||
DummyOssClient dummyOssStub = new DummyOssClient();
|
DummyOssClient dummyOssStub = new DummyOssClient();
|
||||||
OSS oss = mock(OSS.class);
|
OSS oss = mock(OSS.class);
|
||||||
|
|
||||||
doAnswer(invocation ->
|
doAnswer(invocation -> dummyOssStub.putObject(invocation.getArgument(0),
|
||||||
dummyOssStub.putObject(
|
invocation.getArgument(1), invocation.getArgument(2))).when(oss)
|
||||||
invocation.getArgument(0),
|
.putObject(Mockito.anyString(), Mockito.anyString(),
|
||||||
invocation.getArgument(1),
|
Mockito.any(InputStream.class));
|
||||||
invocation.getArgument(2)
|
|
||||||
))
|
|
||||||
.when(oss).putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class));
|
|
||||||
|
|
||||||
doAnswer(invocation ->
|
doAnswer(invocation -> dummyOssStub.getOSSObject(invocation.getArgument(0),
|
||||||
dummyOssStub.getOSSObject(
|
invocation.getArgument(1))).when(oss).getObject(Mockito.anyString(),
|
||||||
invocation.getArgument(0),
|
Mockito.anyString());
|
||||||
invocation.getArgument(1)
|
|
||||||
))
|
|
||||||
.when(oss).getObject(Mockito.anyString(), Mockito.anyString());
|
|
||||||
|
|
||||||
doAnswer(invocation -> dummyOssStub.bucketList())
|
doAnswer(invocation -> dummyOssStub.bucketList()).when(oss).listBuckets();
|
||||||
.when(oss).listBuckets();
|
|
||||||
|
|
||||||
doAnswer(invocation -> dummyOssStub.createBucket(invocation.getArgument(0)))
|
doAnswer(invocation -> dummyOssStub.createBucket(invocation.getArgument(0)))
|
||||||
.when(oss).createBucket(Mockito.anyString());
|
.when(oss).createBucket(Mockito.anyString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user