rename OssApplication and OssController.
parent
7126a8ce33
commit
2f8f0bb28b
@ -0,0 +1,49 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
/**
|
||||
* OSS Application
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class OssApplication {
|
||||
|
||||
public static final String BUCKET_NAME = "spring-cloud-alibaba-test";
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
SpringApplication.run(OssApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AppRunner appRunner() {
|
||||
return new AppRunner();
|
||||
}
|
||||
|
||||
class AppRunner implements ApplicationRunner {
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
try {
|
||||
if (!ossClient.doesBucketExist(BUCKET_NAME)) {
|
||||
ossClient.createBucket(BUCKET_NAME);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("oss handle bucket error: " + e.getMessage());
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.common.utils.IOUtils;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import org.apache.commons.codec.CharEncoding;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* OSS Controller
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RestController
|
||||
public class OssController {
|
||||
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json")
|
||||
private Resource file;
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String upload() {
|
||||
try {
|
||||
ossClient.putObject(OssApplication.BUCKET_NAME, "oss-test.json", this
|
||||
.getClass().getClassLoader().getResourceAsStream("oss-test.json"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "upload fail: " + e.getMessage();
|
||||
}
|
||||
return "upload success";
|
||||
}
|
||||
|
||||
@GetMapping("/file-resource")
|
||||
public String fileResource() {
|
||||
try {
|
||||
return "get file resource success. content: " + StreamUtils.copyToString(
|
||||
file.getInputStream(), Charset.forName(CharEncoding.UTF_8));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "get resource fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
public String download() {
|
||||
try {
|
||||
OSSObject ossObject = ossClient.getObject(OssApplication.BUCKET_NAME, "oss-test.json");
|
||||
return "download success, content: " + IOUtils
|
||||
.readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "download fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue