文章目录 Declare Plugins Introduce Denpendencies OSS Application OSS Config OSS Controller
Declare Plugins
pluginManagement { repositories { gradlePluginPortal ( ) google ( ) mavenCentral ( ) }
} dependencyResolutionManagement { repositoriesMode = RepositoriesMode. PREFER_SETTINGSrepositories { gradlePluginPortal ( ) google ( ) mavenCentral ( ) }
} buildscript { repositories { gradlePluginPortal ( ) google ( ) mavenCentral ( ) }
} plugins { id ( "org.jetbrains.kotlin.jvm" ) version "2.0.21" apply false id ( "org.jetbrains.kotlin.kapt" ) version "2.0.21" apply false id ( "org.jetbrains.kotlin.plugin.spring" ) version "2.0.21" apply false id ( "org.springframework.boot" ) version "3.4.1" apply false
} include ( "alibaba-cloud-oss" )
Introduce Denpendencies
plugins { id ( "org.jetbrains.kotlin.jvm" ) id ( "org.jetbrains.kotlin.kapt" ) id ( "org.jetbrains.kotlin.plugin.spring" ) id ( "org.springframework.boot" )
} java { toolchain { languageVersion = JavaLanguageVersion. of ( 17 ) }
} dependencies { val springBootVersion = "3.4.1" val springCloudVersion = "4.2.0" val springCloudAlibabaVersion = "2023.0.3.2" api ( "io.github.hellogoogle2000:kotlin-commons:1.0.19" ) api ( "org.jetbrains.kotlin:kotlin-reflect:2.0.21" ) api ( "org.springframework.boot:spring-boot-starter: $ springBootVersion " ) api ( "org.springframework.boot:spring-boot-starter-web: $ springBootVersion " ) api ( "org.springframework.cloud:spring-cloud-starter-bootstrap: $ springCloudVersion " ) api ( "com.aliyun.oss:aliyun-sdk-oss:3.18.1" )
}
OSS Application
package x. spring. helloimport org. springframework. boot. autoconfigure. SpringBootApplication
import org. springframework. boot. runApplication@SpringBootApplication
class AliyunOssApplicationfun main ( args: Array< String> ) { runApplication< AliyunOssApplication> ( * args)
}
OSS Config
package x. spring. hello. componentimport com. aliyun. oss. OSS
import com. aliyun. oss. OSSClientBuilder
import org. springframework. context. annotation. Bean
import org. springframework. stereotype. Component@Component
class OssConfig { @Bean fun getOssClient ( ) : OSS { val endpoint = "oss-cn-guangzhou.aliyuncs.com" val accessKey = "LTAI6tABn7ALsMdXJ1jTyQit" val secretKey = "mdy9CpZVZySHSYkCILFsNqs0tg8b6h" val oss = OSSClientBuilder ( ) . build ( endpoint, accessKey, secretKey) return oss}
}
OSS Controller
package x. spring. hello. controllerimport com. aliyun. oss. OSS
import com. aliyun. oss. model. GetObjectRequest
import jakarta. servlet. http. HttpServletResponse
import org. springframework. beans. factory. annotation. Autowired
import org. springframework. core. io. InputStreamResource
import org. springframework. core. io. Resource
import org. springframework. web. bind. annotation. GetMapping
import org. springframework. web. bind. annotation. PostMapping
import org. springframework. web. bind. annotation. ResponseBody
import org. springframework. web. bind. annotation. RestController
import org. springframework. web. multipart. MultipartFile@RestController
class OssController { @Autowired private lateinit var oss: OSS@PostMapping ( "oss/upload" ) fun upload ( bucket: String, name: String, extension: String, file: MultipartFile) : String { val result = oss. putObject ( bucket, " $ name . $ extension " , file. inputStream) val resourceTag = result. eTagreturn "uploaded $ resourceTag " } @PostMapping ( "oss/download" ) fun download ( bucket: String, objectName: String, response: HttpServletResponse) : Resource { val request = GetObjectRequest ( bucket, objectName) val ossObject = oss. getObject ( request) val contentType = ossObject. objectMetadata. contentTypeval resource = InputStreamResource ( ossObject. objectContent) response. contentType = contentTypereturn resource} @ResponseBody @GetMapping ( "oss/query" ) fun query ( bucket: String) : Any { val summaries = oss. listObjects ( bucket) . objectSummariesreturn summaries}
}